Hello everyone. I have some object in the scene, and I want them to move upwards randomly with 1 second break. But the one which moved once will not move again, so engine will not choose the gameObject which it chose before. I made a code for it but it doesn’t work. It chooses all of the objects in array.
var targets : GameObject [];
function Update () {
StartCh();
}
function StartCh(){
yield WaitForSeconds(1);
for ( var target : GameObject in targets )
{
target.GetComponent(MoveUpScript).moveUp=true; // move the choosen object up.
}
}
Thanks 
Instead of having a for - which goes over all the objects in your array one by one and applies whatever is in the block to them, which is why you’re getting that behavior - access the array at a random index using Random.Range(0, targets.Length)
. Also don’t call a co-routine from update, it’ll just keep piling them on top of each other. Instead have an infinite loop on Start or in the co-routine itself. Your code should look something like this:
var targets : GameObject [];
function Start ()
{
StartCh();
}
function StartCh()
{
while(true)
{
targets[Random.Range(0, targets.Length)].GetComponent(MoveUpScript).moveUp=true;
yield WaitForSeconds(1);
}
}