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
1
Question by kevinwong42 · Nov 18, 2013 at 06:54 AM · waitforseconds

WaitForSeconds() equivalent for void functions?

Hi all,

I love using the yield return new WaitForSeconds() function in my IENumerator functions and I need the same kind of functionality in a void function. is there an equivalent piece of functionality that I can use?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Bunny83 · Nov 18, 2013 at 01:06 PM

The never ending story ;)

Unity runs the whole scripting environment and most parts of the engine in a single thread. A "normal" function is like an atomic function. If you would stop inside the function for lets say 3 seconds, your whole application would stop for this time.

For example you could use:

     System.Threading.Thread.Sleep(3000); // 3 sec.

inside your method, but again it will freeze your whole application for this time. Nothing else can happen in your app during this time.

Coroutines are actually not methods in the usual sense. There is a lot magic going on in the background. Each started coroutine is actually an object and Unity "iterates" over your code pieces between your yields. Sounds strange but it's actually quite simple.

So the straight answer is: There is no equivalent.

Oh, i almost forgot to link UnityGems ;)

Comment
Add comment · Show 4 · 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 etrhos · Dec 29, 2016 at 01:47 AM 0
Share

Thank you so much for explaining this. Your firs paragraph really helped me understand the root of my issue =D

avatar image HanSoloYolo · Aug 15, 2017 at 02:42 AM 0
Share

I wanted a short delay before my method called the Scene$$anonymous$$anager to change scenes. Your answer is the cleanest, simplest, and most direct solution to that issue.

Thank you very much for posting a SI$$anonymous$$PLE ANSWER! =) $$anonymous$$uch appreciated!

avatar image hunterrocks777 · Oct 04, 2021 at 10:23 AM 0
Share

Hey um, the UnityGems thing goes to malware. DO NOT CLICK IT! And Original Commenter, if you are still there, can you remove it? Thanks

avatar image andrew-lukasik hunterrocks777 · Oct 04, 2021 at 11:16 AM 0
Share

I updated the links so they point to web archive now.

avatar image
3

Answer by GameVortex · Nov 18, 2013 at 11:31 AM

You could try Invoke to delay the calling of a function in your void function: Invoke

Example:

 void Step1 ()
 {
     Invoke( nameof(Step2) , 2.0f );
 }

 void Step2 ()
 {
     // this function will be called with a delay of 2 seconds after the Step1
 }
Comment
Add comment · 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
1

Answer by Landern · Nov 18, 2013 at 02:03 PM

Couldn't you just make a helper method?

 // ... Some other method that has exposure to helper method
 StartCoroutine(Wait(2.5f));
 // .. end
 
 public IEnumerator Wait(float delayInSecs)
 {
   yield return new WaitForSeconds(delayInSecs);
 }
Comment
Add comment · 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
-1

Answer by TalkingBlah · Oct 04, 2021 at 08:31 AM

hi this maybe a stupid idea but you can make a custom wait for function like this in the picture and also can modify the flow of execution using goto making it maybe similar to co routines.

[1]: /storage/temp/187032-monkey-mask-microsoft-visual-studio-10-4-2021-1-53.png


monkey-mask-microsoft-visual-studio-10-4-2021-1-53.png (83.2 kB)
Comment
Add comment · Show 3 · 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 andrew-lukasik · Oct 04, 2021 at 08:54 AM 0
Share

while(true) blocks the whole application and nothing else.

avatar image TalkingBlah andrew-lukasik · Oct 04, 2021 at 09:30 AM 0
Share

nah the idea is stupid but the while(true) will turn false or the break statement will break out of the loop when this statement [if ((currentTime - startTime) >= waitTime)] evaluvates true;

avatar image Eric5h5 · Oct 04, 2021 at 08:56 AM 1
Share

Why in the world would you post that as an image? Post code as, you know, code. Anyway, yes, that's a very bad idea.

avatar image
0

Answer by DaveA · Nov 18, 2013 at 06:58 AM

like

yield return null;

??

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 Eric5h5 · Nov 18, 2013 at 07:22 AM 0
Share

yield requires a function to return IEnumerator. yield return null waits for a single frame.

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

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

check bool in WaitForSeconds 2 Answers

Coroutines with Yield return new waitforseconds(); go for execution of other functions? 0 Answers

Instantiate color change loop 0 Answers

run level for 4 minutes help 1 Answer

Corountines and messaging system 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