- Home /
How to generate prefab with random number inside it ?
I want to generate balls with random number inside it using C# & 2D. Please check the attached image file. Thanks :)
Answer by invo1 · Mar 05, 2018 at 11:36 AM
U need to make the prefab to have a text component on it,then in the script GameObject go=Instantiate(myPrefab); int number =(int)Random.Range(0,100); myPrefab.GetComponent().text = number.ToString();
Just a $$anonymous$$or correction for the last instruction:
go.GetComponent<Text>().text = number.ToString();
Another clarification: int number = (int)Random.Range(0, 100);
will use the version with int
ranges, so you don't need to cast to int, but you will also never get 100, because it is inclusive-exclusive. So either do int number = (int)Random.Range(0, 100f);
or int number = Random.Range(0, 101);
Your answer
Follow this Question
Related Questions
UI components all disappear on Play mode, then stay invisible 2 Answers
Random Text to Text Mesh? 1 Answer
How can I change textmesh to a random text snippet? 1 Answer
Text missing in Editor; huge amounts of bugs and glitches 0 Answers
How can I make my prefab fire out at a random velocity every 2 secs 2 Answers