- Home /
About WaitForSeconds
I want to live a GameObject for some seconds before destroy it.I saw the code by c# like this in documentation: IEnumerator Awake() { print(Time.time); yield return new WaitForSeconds(5); print(Time.time); } how to use this code? If I put it in my code like this: void update(){ ...... Awake(); ...... }
It is not called.Noting happen!
If I put it like this void update(){ ..... yield return new WaitForSeconds(5); ...... } There is a error like this : error CS1624: The body of demo.Update()' cannot be an iterator block because
void' is not an iterator interface type
If I put it like this: void update(){ ..... yield new WaitForSeconds(5); ...... } There is a red line .
How to use this WaitForSeconds?Thanks!
Answer by Waz · Aug 03, 2011 at 06:28 AM
Or just:
Destroy(theGameObject,5);
You could queue the animation to start in 5 seconds. In Javascript, yield is actually really easy too:
function DieSoonThenExplode()
{
yield WaitForSeconds(5);
otherObject.animation.Play("explosion");
Destroy(gameObject);
}
The C# isn't much difference, but if you're struggling with it, I'd suggest using Javascript.
I want to know the difference between c# and Js.Because I dont know where the function I should put.When I put it in update(),it dont work like having not be called.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Issues rotating around transform.up 2 Answers
C# WaitForSeconds isn't waiting for any seconds 2 Answers
Trying to make WaitForSeconds work 3 Answers