Quantcast
Viewing latest article 1
Browse Latest Browse All 4

Answer by Bunny83

You don't understand how coroutines works. When calling RandomPowerup in Update the function returns immediately and just starts a new coroutine that runs on its own. Since you start a new one each frame you will have hundreds of them running at the same time.

At a frame-rate of 100fps (assumed that the random goes way 1 or 2 and not 0) you have a total waiting time of 8 sec which will run 800 instances of this function at the same time.

You have to prevent calling the function again when it's still running. The easiest way is to use a boolean.

var RandomPowerup_running = false;
function Update ()
{
    RandomPowerup();
}
function RandomPowerup()
{
    if (RandomPowerup_running)
        yield break; // Not sure if that's the right syntax in UnityScript, i'm a C# user ;)
    RandomPowerup_running = true;
    yield WaitForSeconds(5);
    //
    // ...
    //
    RandomPowerup_running = false;
}

Viewing latest article 1
Browse Latest Browse All 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>