- Home /
Delay between each instantiation
Hi guys. I'm kind of new to this. I want cubes to spawn randomly in the game every, let's say, 5 seconds. I have this code so far:
var object:Transform; var x = 12; var z = 12; var y = 5;
function Update () {
var position = Vector3(Random.Range(-x, x), y, Random.Range(-z, z));
Instantiate(object, position, Quaternion.identity);
}
I tried using yield WaitForSeconds(5.0) but it messed things up... Thank you in advance.
~Bumbaz
Answer by AliAzin · Sep 15, 2010 at 04:26 PM
you can't use yield in Update(). you should do it in start(), Awake() or custom functions like this:
function Start(){ DoInstantiate();
} function DoInstantiate(){ while (true){ yield WaitForSeconds(5.0); var position = Vector3(Random.Range(-x, x), y, Random.Range(-z, z)); Instantiate(object, position, Quaternion.identity); }
}
Answer by illinar · Dec 19, 2012 at 08:43 AM
C# code I would recommend:
public GameObject spawnObject;
public float xRange;
public float zRange;
public float y;
public float spawnRate;
void Awake ()
{
InvokeRepeating("Spawn", 0f, spawnRate);
}
void Spawn ()
{
Instantiate(spawnObject, new Vector3(Random.Range(-xRange, xRange), y, Random.Range(-zRange, zRange)), Quaternion.identity);
}
Answer by unityart3ds · Dec 19, 2012 at 07:53 PM
ok in java script for unity this is how you get the script to spawn a item and when it gets destroyed to let it spawn agein.
var bullet : GameObject;
var bulletbackup : GameObject;
function Update () { if( Input.GetButton("fire1"))
{bulletbackup = Instantiate(bullet, transform, rotation,))}
}
Now to make a spawn time on a object is like this
var spawntime : float = 1;
var resetnumber : float = 10;
function Update (){Time.deltaTime * ++spawntime;
}
That might help but the best way Ive made a spawn time is by useing the animation tool
But I hope this helps.
Oh and im not for sure on the script with the spawn time but that is done in Java
Your answer
Follow this Question
Related Questions
Delay in Instantiate only runs once 2 Answers
How to delay a respawn? 1 Answer
why does Coroutine 'yield return 0' sometimes take a lot of frames before it continues 1 Answer
Respawn after delay 3 Answers
Delay If statement in Update c# 2 Answers