Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MuffinMyst · Nov 23, 2014 at 05:42 AM · delay

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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..
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MuffinMyst · Nov 25, 2014 at 06:14 AM 0
Share

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);
     }
avatar image
1

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 23, 2014 at 06:28 AM 0
Share

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.

avatar image Jeff-Kesselman · Nov 23, 2014 at 06:28 AM 0
Share

Good point :)

avatar image MuffinMyst · Nov 23, 2014 at 06:32 AM 0
Share

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.

avatar image MuffinMyst · Nov 23, 2014 at 06:48 AM 0
Share

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.

avatar image Bluestrike · Nov 23, 2014 at 08:27 AM 0
Share

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()






Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges