简体   繁体   English

分配给文本组件的随机数组字符串-Unity 4.6,uGUI

[英]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?) 类型“ UnityEngine.Random”不包含“下一个”的定义,找不到类型为“ UnityEngine.Random”的扩展方法“下一个”(您是否缺少using指令或程序集引用?)

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..). 看起来,当您不使用它的全名时,您的Random将会引用UnityEngine.Random而不是System.Random类(因为您已经在名称空间中使用了它,等等。)。

Use it's full name as; 将其全名用作;

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

or change your UnityEngine.Random name to something else (which I suggest). 或将您的UnityEngine.Random名称更改为其他名称(建议这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为文本组件分配随机数组字符串-Unity 4.6,uGUI - Assign a random array string to a text component - Unity 4.6, uGUI 将纹理数组加载到单个图像组件中,并放入Unity 4.6中的网格布局中(使用uGUI) - Load array of textures into individual Image components into a grid layout in Unity 4.6 (using uGUI) 单击按钮时,生成GameObject并从纹理数组设置其图像-Unity 4.6(使用uGUI) - Generate GameObjects and set their image from an Array of Textures when a button is clicked - Unity 4.6 (using uGUI) 如何在Unity中将文本组件更改为字符串 - How to change a text component to a string in Unity Unity 4.6+通过脚本创建文本 - Unity 4.6+ Creating Text through a Script 在GetKeyDown(Unity 4.6)上切换Text(script)的呈现 - Toggling the rendering of Text(script) on GetKeyDown (Unity 4.6) Unity 4.6无法将类型'UnityEngine.Component'隐式转换为'DamageInterface' - Unity 4.6 Cannot implicitly convert type `UnityEngine.Component' to `DamageInterface' Unity & C# - 从数组中选择一个随机字符串 - Unity & C# - Choosing a random string from an array unity 字符串中的随机字符 - Unity random characters in string 在Unity 4.6中的文本冒险中遇到状态更改问题 - Having issues with State changes in a Text Adventure in Unity 4.6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM