- Home /
How to my objects randomly active?
I have a GuiButton and 3 Objects(Ball1, Ball2, Ball3). All inactive. If I press left mouse button on GuiButton, then Randomly active Ball1 or Ball2 or Ball3.
How can I do this?
Answer by Sonaten · Oct 25, 2012 at 07:35 PM
You can easily use the Random.Range() function. If you make sure that it is cast as int, you can use it as an index to handle your objects.
Either put the objects in a List of GameObjects in one of your scripts. Or, use a parent for the objects. Do something like this.
int i = (int)Random.Range(0f,3f);
parentObject.transform.GetChild(i).gameObject.active = true;
I don't believe you have to use floats and a cast. But I have just gotten the habit of it.
I hope this helped a bit.
Thanks for your answer!:) But unfortunately I can't use this. :/
Your answer
Follow this Question
Related Questions
Random time activate/deactivate object 1 Answer
Function action applies to every object that have script with that function on it. Help please 1 Answer
Make an object move towards random spot on another objects edge? 1 Answer
Count >= then object tag becomes active 1 Answer
How to enable object by clicking on the button through the Massive(array)? 0 Answers