A very general programming question

Hey so I’m having trouble with understanding arrays. I know this is more of a Javascript question than a Unity one, but if you could bear with me I’d appreciate it. I made a simple video that asks the question in detail.

http://www.youtube.com/watch?v=nGgUNaiVJfg&feature=youtu.be

Don’t worry about the code I have there - it is simply a fragment of the last thing I tried and doesn’t make any sense.

Thanks guys.

What is your code, then, and what is the actual problem? The video doesn’t really address this. The problem with the code you have there is that Update runs every single frame and can’t be interrupted or delayed by using yield. If you don’t want something to occur every frame, use something other than Update, like InvokeRepeating or a coroutine.

–Eric

You can’t yield in the Update function.
What I would do as a quick fix to this without getting complicated is something like this

var canSpawn : boolean = true;

function Update()
{
     if(canSpawn)
     {
          canSpawn = false;
          SpawnRoach();

     }
}

function SpawnRoach()
{
      yield WaitForSeconds(3);
      // Spawn roach and search array here, or however you had it
      canSpawn = true;
}

This way, a roach can only spawn when the boolean is true, and it will get overlooked in the Update until those three seconds pass.

Understand the Logic? Hope this helps!
It’s a simple and easy to understand fix.

Thanks so much guys! I understand!

If you don’t know much about Invoke and Invoke Repeating, chitanblue’s example above can be done like the below example with less lines and no vars.

function Start() {
    //------------//
    InvokeRepeating("FunctionName", 3, 3);
    //------------//
}
function FunctionName() {
    //------------//
    // Wash my Car
    //------------//
}

@ chitanblue’s - I still do that style sometimes too. :sunglasses: