- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Alp-Giray-Savrum · Jan 31, 2014 at 01:01 PM ·
randomcoroutineupdatespawnwaitforseconds
How to make a spawner wait for x seconds?
Hello guys. I don't know how to do it but here's my code;
//This is array List;
var randomObject : GameObject[];
function Update () {
var randomSelect = Random.Range(0,(randomObject.length));
var position = transform.TransformPoint(Random.Range(-3.704676, 5.132769), 0, Random.Range(-3.704676, 5.132769));
Instantiate(randomObject[randomSelect], position, Quaternion.identity);
}
So, in a manner I want to coroutine this event. but wait for random ! like wait for 3 secs, later wait for 5 secs, later wait for 0 secs etc... But I can't coroutine Update Function ! What can i do ? Arigato.
Comment
Answer by james_170482 · Jan 31, 2014 at 04:18 PM
You Could try
public float RandomTime;
void Awake()
{
StartCoroutine( RandomSpawn() );
}
void Update()
{
RandomTime = Random.Range(1, 100);
}
IEnumerator RandomSpawn()
{
while( true )
{
//Spawn
yield return new WaitForSeconds( RandomTime );
}
}
Maybe a Solution;
Answer by michielbos · Jan 31, 2014 at 01:39 PM
Well, if you made a new function, you could use the WaitForSeconds() there.
try something like this:
var spawnReady = true;
function Update () {
if (spawnReady)
Spawn();
}
function Spawn () {
spawnReady = false;
yield WaitForSeconds(5);
var randomSelect = Random.Range(0,(randomObject.length));
var position = transform.TransformPoint(Random.Range(-3.704676, 5.132769), 0, Random.Range(-3.704676, 5.132769));
Instantiate(randomObject[randomSelect], position, Quaternion.identity);
spawnReady = true;
}
Wow, assigning to private function works :D Thanks !