Controlling spawn Time.

Can someone please post an example of a script that will continually spawn something every few seconds?

Thanks.

well, something like this would work

var isTrue = true;

function Update () {
     if (isTrue) {
            Spawn();
     }
}

function Spawn () {
     isTrue = false;

     yield WaitForSeconds(waitTime);
     // instantiate function stuff
     isTrue = true;
}

I think this’ll work, spawning something every couple seconds or so.

function Update () {
var mytime = time.Time % 2;

if(mytime == 0)
{
variable = Mathf.Floor(Random.Range(0, 2));

if(ob ==1)
	{
		var thing1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);

	}
	else
		{
			var thing2 =GameObject.CreatePrimitive(PrimitiveType.Cube);
		}
	}
}
var spawnInterval : float = 5.0;

InvokeRepeating("MySpawnFunction", spawnInterval);

function MySpawnFunction(){
    //spawn stuff
}

Well, more specifically I want it to instantiate every few seconds. How would I do that?

If you look at my above code, InvokeRepeating will call the function MySpawnFunction every 5.0 seconds. You would just put your instantiation code inside the function.

Not sure I understand that. Can you do something with this?

var farMovingTarget : Transform;

function Update () {
  var createTarget = Instantiate(farMovingTarget, GameObject.Find("farMovingTargetSpawn").transform.position, Quaternion.identity);
}
var farMovingTarget : Transform;
var spawnInterval : float = 5.0;

InvokeRepeating("SpawnTarget", spawnInterval);

function SpawnTarget(){
    var createTarget = Instantiate(farMovingTarget, GameObject.Find("farMovingTargetSpawn").transform.position, Quaternion.identity);
}

That doesn’t work. I get this error:

Assets/Scripts/spawnFarMovingTarget.js(4,16): BCE0017: The best overload for the method ‘UnityEngine.MonoBehaviour.InvokeRepeating(String, float, float)’ is not compatible with the argument list ‘(String, float)’.

Oh, sorry, the InvokeRepeating line should be:

InvokeRepeating(“SpawnTarget”, spawnInterval, 0.0);

It’s getting there. It just spawns once and then stops. I want it to keep going.

try mine?

I don’t really understand yours.

Whoops, had it backwards, lol. should be:

InvokeRepeating(“SpawnTarget”, 0.0, spawnInterval);

Thank you so much. It works perfectly.