简体   繁体   中英

Random array string assigned to a text component - Unity 4.6, uGUI

Need some help with this one. I'm creating an array with a list of descriptions (strings) that I need to choose randomly and then assign to a text component in a gameobject.

I feel like I'm close but I'm getting an error:

Type `UnityEngine.Random' does not contain a definition for `Next' and no extension method `Next' of type `UnityEngine.Random' could be found (are you missing a using directive or an assembly reference?)

What should I be using instead of 'Next'?

public Text myText;
Random rand = new Random();

public string[] animalDescriptions = 
{
    "Description 1",
    "Description 2",
    "Description 3",
    "Description 4",
    "Description 5",
};

void Start()
{
    string myString = animalDescriptions[rand.Next(animalDescriptions.Length)];
    myText.text = myString;

    Debug.Log (myString);
}

Looks like when you don't use it's full name, your Random will be referring to UnityEngine.Random not System.Random class (because you have already used this in your namespace etc..).

Use it's full name as;

System.Random rand = new System.Random();

or change your UnityEngine.Random name to something else (which I suggest).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM