- Home /
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?
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 ;)
Thank you so much for explaining this. Your firs paragraph really helped me understand the root of my issue =D
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!
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
I updated the links so they point to web archive now.
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
}
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);
}
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
while(true)
blocks the whole application and nothing else.
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;
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.
Answer by DaveA · Nov 18, 2013 at 06:58 AM
like
yield return null;
??
yield requires a function to return IEnumerator. yield return null waits for a single frame.
Your answer
Follow this Question
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