Hi, can someone tell me what I’m doing wrong? I want the rigidbody to be re-created every 3 seconds or so (its a variable as you can see).
var projectile : GameObject;
var WaitTime : int = 3;
yield new WaitForSeconds (WaitTime);
function Update () {
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
}
you would start a coroutine in the “start” method of the object and yield the coroutine
your way of doing it does not work at all, as the yield is not in the update, so its ignored. and update is called on each frame.
you would have to measure time differences to make that work for example
That’s because Update() is run every frame. If you put your yield in there, you would have 3 seconds worth of Update functions running side-by-side (and still wouldn’t work the way you want it to).
As the previous poster said, put these in Start(). That way, it will be executed once.
However, I now need a way to loop this. I now I’ve seen it somewhere but I was searching the forums and all I could find was how to loop animations, not scripts.
EDIT:
Never-mind, I got it to work!
Thanks a lot guys!
For all of you who have a similar problem, here’s the final script:
var projectile : Rigidbody;
var timer = 1;
function Start () {
while(true) {
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
yield new WaitForSeconds (timer);
}
}