- Home /
What function to use, for this statement?
I use function Update in this script, but i need to use Waitforseconds. But as you know i cant use it in Update. Which function can i use?
#pragma strict
var rndNum:int;
var less = 5;
var asteroid : Transform;
var myposition : GameObject;
function Start () {
}
function Update () {
var rndNum: int = Random.Range(1, 100);
if(rndNum < less)
{
Instantiate(asteroid, myposition.transform.position, myposition.transform.rotation);
Debug.Log(rndNum);
}
}
Uhm... where in your code do you use / want you use Waitforseconds? And what's the purpose?
I guess a simple coroutine with "while(true)" would do what you want, but it's just a guess since we don't know what you want.
I want to use it after the Instantiate line, so the fuction replays after some seconds. I shoudl probably also use a loop.
But the script does that it spawns a prefab when rndNum < less.
Answer by Bunny83 · Feb 13, 2014 at 07:48 PM
Update is called every frame, so it makes no sense and won't work to delay Update. You can use a coroutine instead:
function SpawnAsteroids() { while(true) { var rndNum: int = Random.Range(1, 100);
if(rndNum < less)
{
Instantiate(asteroid, myposition.transform.position, myposition.transform.rotation);
}
yield WaitForSeconds(0.1f);
}
}