How to randomize a single action among many objects PLEASE help!
Hey everyone I was wondering if anyone can point me in the right direction on how to go about this situation:
It is a 2d game so far with 3 triangles. I assigned the iTween Move script to one of them. When I play the game, the triangle assigned moves up and back to its original position. I was wondering if there is a way to randomize that particular script action to all 3? Meaning every time I hit play, the script is at random on one of the triangles. How would I go about doing this? Do I make a separate script? Do I assign the same script to every object and then somehow randomize which one comes on? I would greatly appreciate advice or directions. Thank you!
Answer by EmHuynh · Mar 05, 2016 at 12:18 AM
Hello, @Jalah_LLC.
We can declare an array for those triangles. Then we call Random.Range to randomly pick a triangle from the array and add the iTween Move component to it.
public class Example : MonoBehaviour
{
public GameObject[] triangles;
void Start() {
triangles[ Random.Range( 0, triangles.Length ) ].AddComponent< iTween >();
}
}