WaitForSeconds not working?

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

Yes, I originally had it inside the Update function but the error console said (and I quote)

“Script error: Update() can not be a coroutine.”

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.

Okay, I don’t know if I’m missing something or what, but here’s what I have and it’s still creating another RigidBody every frame:

var WaitTime : float = 2.0;
var projectile : GameObject;

function Start () {
	yield WaitForSeconds(WaitTime);
}

function Update () {
	var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

    Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
}

(I’m doing this with Unity iPhone Basic if that changes anything, I don’t think it would but just incase)

Hallo,
i dont know whats wrong with yours but this here works and maybe you can use for yours to build in:

var projectile : Rigidbody; 
var timer = 3;

function Start () { 

while(true) { 
	
	var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation); 

	yield new WaitForSeconds (timer); 

	//Destroy(instantiatedProjectile, 2);

	} 
}

EDIT: the Destroy function should be detached in an own script, so that it doesnt depend on the yield waitforseconds timer

Thanks Achim! This worked!

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);
	}
	
}

For the record, you can also use InvokeRepeating (see the example script here):