Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jishmael · May 30, 2016 at 01:21 PM · coroutinewaitforsecondsdelay

Coroutine confusion

Although this may have already been answered elsewhere, I am still struggling to create delay's in code execution and apparently Coroutines are the best way to do this. In my example: Previously written main code, At a point where I decide I need to pause until continuing , Proceeding code.

I have tried using coroutines for the delay but I don't want to have to put all of the code following the delay into the coroutine, just pause the execution of my main code before continuing with it.

I do understand that this is not the clearest explanation but any help is appreciated.

Comment
Add comment · Show 1
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 Hellium · May 30, 2016 at 12:50 PM 0
Share

For simple delays, a simple Invoke can be enough.

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html

https://unity3d.com/learn/tutorials/topics/scripting/invoke

1 Reply

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

Answer by DiegoSLTS · May 30, 2016 at 01:32 PM

You can't pause the execution of a normal function unless you use threads, but threads are not recomended in Unity since doing things outside the main thread is complicated.

If you don't want to move all the code into the coroutine you can do this. Let's say this is your code:

 void SomeFunction() {
     //some code
 
     //you want a delay here
 
    //some more code
 }

Turn that into:

 void SomeFunction() {
     //some code
 
     StartCoroutine(Delay(2f,SomeMoreCode)); //delay of 2 seconds, pass the value you want
 }
 
 void SomeMoreCode() {
     //some more code
 }
 
 IEnumerator Delay(float seconds,UnityAction callback) {
     yield return new WaitForSeconds(seconds);
     callback();
 }

This way you can delay any function for any amount of seconds from any other function in your code.

Comment
Add comment · Show 8 · 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 Hellium · May 30, 2016 at 01:37 PM -1
Share

This way you can delay any function

Not really with the code you have given. Only functions that do not require arguments. The Invoke way would be easier here :

  void SomeFunction() {
      //some code
  
      Invoke("Some$$anonymous$$oreCode", 2f) ;
  }
  
  void Some$$anonymous$$oreCode() {
      //some more code
  }


avatar image DiegoSLTS Hellium · May 30, 2016 at 01:53 PM 0
Share

I always prefer to do things that don't require using the names of methods as strings, it's easier (not much here, just a 2 line function) but more prone to errores in the future.

Anyway, I wouldn't do a coroutine that only delays and don't use Invoke for the string parameter, I'd just make "SomeFunction" a coroutine, do the first part, yield return for some seconds and do the second part.

You're right about the "any function", the callback can't receive parameters.

avatar image Hellium DiegoSLTS · May 30, 2016 at 02:12 PM 0
Share

Yes, I also avoid using string to call a function. It's more difficult to know where my function is called, and who calls it.

Ins$$anonymous$$d of using a Unity action, a custom delegate could be a good choice.

avatar image Jishmael Hellium · May 30, 2016 at 02:14 PM 0
Share

Is there no way to say, Invoke to a function after 'x' time that simply logs something to the console and then bounce back to where Invoke was called and continue?

avatar image Hellium Jishmael · May 30, 2016 at 02:22 PM 0
Share

Unfortunately, this behaviour is not possible using Invoke. Only coroutines are possible here. See DiegoSLTS's answer ins$$anonymous$$d.

Show more comments
avatar image Jishmael · May 30, 2016 at 02:34 PM 0
Share

Apparently, UnityAction as a namespace could not be found.

avatar image DiegoSLTS Jishmael · May 30, 2016 at 02:41 PM 0
Share

Yes, sorry. UnityAction is in the UnityEngine.Events namespace. Either add that as a using at the top or write UnityEngine.Events.UnityAction ins$$anonymous$$d of UnityAction as the coroutine parameter type.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create a delay in a function being called in update? 2 Answers

Coroutine not working as expected 1 Answer

How can i get this Delay script to work? 1 Answer

Coroutine not running after yield return new WaitForSeconds 3 Answers

Can IEnumerator be called more than once? 1 Answer


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