Random Spawn

Basically what I am trying to achieve is spawn an object at an interval of 1-2 minutes and also increase that interval in time; let’s say after 30 minutes of play the interval between the spawns to be increased to 3-4 minutes. Here’s the wee code I came up to (incomplete) - and this doesn’t even do the first part of the job :slight_smile:

var BonusLifeTarget : Transform;

function FixedUpdate () {
   	BonusLifeSpawn();
}

function BonusLifeSpawn(){
	yield WaitForSeconds (Random.Range(10, 100)) ; 
    Instantiate (BonusLifeTarget);
}

Any suggestion is very much appreciated (or links to some learning)

Forget yield (linky link as you asked). If the script is to be running in the background somewhere else anyway, use Time.time:

var BonusLifeTarget : Transform;

var lastSpawn : float;
var interval : float;

function Start () {
    ResetBonusLifeInterval();
}

function FixedUpdate () {
    BonusLifeSpawn();
}

function ResetBonusLifeInterval () {
    lastSpawn = Time.time;
    interval = Random.Range(10, 100);
    if (Time.time > 30 * 60) { // 30 minutes
        interval = Random.Range(100, 200);
    }
}

function BonusLifeSpawn () {
    if (lastSpawn + Time.time > interval) {
        ResetBonusLifeInterval();
        Instantiate(BonusLifeTarget);
    }
}

I’m just not too sure if this is proper javascript because I’m used to csharp! :wink:

If you want to insist in using yield, the code does become clearer, but I like to avoid yield due to bad experiences with it.

So, again, I don’t know if this will work as expected:

var BonusLifeTarget : Transform;

function FixedUpdate () {
    YieldBonusLifeSpawn();
}

function YieldBonusLifeSpawn(){
    yield StartCoroutine("BonusLifeSpawn");
}

function BonusLifeSpawn(){
    yield WaitForSeconds (Random.Range(10, 100)) ; 
    Instantiate (BonusLifeTarget);
}