- Home /
I don't understand "WaitForSeconds"
I've tried numerous times to look this up. I still don't seem to get how to use it. I want to be able... in whatever part of my script I want... to wait for a specific number of seconds. But when I try using the "WaitForSeconds" it usually gives me an error.
example of the script
void Death(){ //basically I'll call this when health <0
death.Play(); //my critters death cry
deathParticles.active = true; //burst of particles
WaitForSeconds(2); // here I want to wait to give the sound and particles time to play out before the destroy
Destroy(this.gameObject);
}
I've tried "yield return new WaitForSeconds(2); but that makes it so the "void" has to be taken off the "Death" then I get other errors if I try to do that... if I don't include the "yield return" part it tells me "expression "denotes a 'type' where a 'variable', 'value' or 'method group' was expected.
So... how can I make a script wait for a number of seconds? I don't know what I'm doing wrong...
Answer by Jignesh G. · Nov 23, 2014 at 06:31 PM
Below code should work properly on c#
void Start()
{
ExecuteAfterDelay (3F);
}
IEnumerator ExecuteAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
//some code goes here..
}
OR you can use invoke method with poassing required delay in it.
void Start()
{
Invoke("ExecuteAfterDelay", 3F);
}
void ExecuteAfterDelay()
{
//some code goes here..
}
that first one i had tried and no go... and the second one I thought I had tried..., but didn't work for some reason... perhaps... i had invoked an IEnumerator method (I'm not sure why it would cancel out any delay... but)*dit:(it could be that i was using an integer?) But testing it as you wrote it the second one had the delay :D so yey!!
I did try the first method again... and for some reason it isn't even calling Delay (or at least... the debug logs inside it isn't printing...)
He's what I have for the code. (and thank you for the answer!!!)
public void On$$anonymous$$ouseDown(){
Debug.Log ("time before delay is: " + Time.time);
Delay(3F);
}
IEnumerator Delay(float delay){
Debug.Log ("time in delay is: " + Time.time);
yield return new WaitForSeconds (delay);
Debug.Log ("time in delay after waitfor is: " + Time.time);
}
Answer by Jeff-Kesselman · Nov 23, 2014 at 05:42 AM
You cannot just pause in the middle of a script. That would lock your entire game up.
You need to launch a co-routine so you dont block the rest of the program.
Look here:
http://docs.unity3d.com/Manual/Coroutines.html
A slightly easier but more limited solution is to use Invoke
http://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
In addition to that answer: In this specific case you don't need a coroutine since the Destroy method can take a second parameter where you can specify a delay. So by using:
Destroy(gameObject, 2);
it will destroy the gameobject in 2 seconds.
Of course if you want to do other things after the 2 seconds you have to use a coroutine or Invoke a seperate method which does those things.
I've figured out that I can make an IEnumerator class, (I named it delay) that takes in an int value a, and then uses the yield return new waitForSeconds(int a);
In my code I've added debug logs (example:)
void Death(){ //basically I'll call this when health <0
Debug.log("about to play death cry");
death.Play(); //my critters death cry
deathParticles.active = true; //burst of particles
Debug.log("about to delay " + Time.time);
Delay(2); // here I want to wait to give the sound and particles time to play out before the destroy
Debug.log("after delay " + Time.time);
Destroy(this.gameObject);
}
IEnumerator Delay(int a){
yield return new WaitForSeconds (a);
}
when I play/test I get the critters health to < 0... and it just destroys it without playing the sound or the particles... (I have been playing with it a bit, so I know the sound and particles do in fact work... taking out the destroy part it will play the sound and the particles.... but with the destroy (even after the delay) it will just destroy it without doing the two things before the delay. The Debug logs print out... the time displayed has no change between them... (7.876052 on both lines)
so... getting closer... but still not getting any delay/wait effects.
Ah Bunny! that did the trick for this... Destroy(this.gameObject, 2); did indeed wait :D
(though I would still like to know how to delay at some point...) why would it not show a difference in time when I print the time before and after I call "Delay"
I've tried calling it as a Coroutine (using both startCoroutine and simply Coroutine... and even Invoke) and still there was absolutely no difference in the two printed times.
That is because the printed times were not delayed. There is no print in the delay function and the death function is no IEnumerator but void. A void function can't yield. And Delay() had no yield in front so the code is not waiting for delay to complete first.
Solution 1:
You would have to place all code after delay in the delay function.
Solution 2:
$$anonymous$$ake the Death function an IEnumerator and do
yield Delay().
As your delay function is basicly a copy of WaitForSeconds, the delay can be replaced with WaitForSeconds then
yield WaitForSeconds()
Your answer
Follow this Question
Related Questions
Add delay in while loop 1 Answer
How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers
android audio delayed 1 Answer
cooldown timers 0 Answers
Shoot Bullet Delay Enemy 0 Answers