Activate Random Objects

Hi, I have 10 (Null) objects and I want to activate them as Score Object or Death Object, but I want to activate them randomly at random time between 5-15 seconds.

I thought to make 2 scripts one for score and one for death and use something like this to enable one of the script. but how can I achieve it randomly?gameObject.GetComponent<Score> ().enabled = true;
1893750--121928--Help.jpg1893750--121929--unity help.jpg

LE: Or a better way should be to randomly generate a number from 1-10 and if the number = Object name randomly activate one of the scripts on that object?

You could try this. Though it’s in Unity’s javascript.

var score : GameObject;
var death : GameObject;
var gamearray : GameObject[];

function Start()
{
gamearray = new GameObject[10];
InvokeRepeating("ChangeArray", Random.Range(5, 15), Random.Range(5, 15));

for(var i : float = 0; i < 10; i++)
{
    var chooseRandomly : boolean = (Random.value < 0.5);
    if(chooseRandomly == true)
   {
       gamearray[i] = score;
   }
   else
   {
       gamearray[i] = death;
   }
}
}

function ChangeArray()
{
for(var i : float = 0; i < 10; i++)
{
    var chooseRandomly : boolean = (Random.value < 0.5);
    if(chooseRandomly == true)
   {
       gamearray[i] = score;
   }
   else
   {
       gamearray[i] = death;
   }
}
}
1 Like

This way you will have an array of gameobjects that changes every 5-15 seconds with randomly changed score or death at positions 0-9. I’ll gladly help you if there’re other problems.

1 Like

Thank you! I was in a little vacation, I will test this today or tomorrow. :smile: