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?
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?
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 ![]()
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
}
like
yield return null;
??
yield requires a function to return IEnumerator. yield return null waits for a single frame.
– Eric5h5Couldn’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);
}
Remember to post comments as actual comments by using the "Add new Comment" button (I converted this one for you).
– GameVortexhi 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.
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.
– Eric5h5
Thank you so much for explaining this. Your firs paragraph really helped me understand the root of my issue =D
– etrhosI wanted a short delay before my method called the SceneManager to change scenes. Your answer is the cleanest, simplest, and most direct solution to that issue. Thank you very much for posting a SIMPLE ANSWER! =) Much appreciated!
– HanSoloYoloHey um, the UnityGems thing goes to malware. DO NOT CLICK IT! And Original Commenter, if you are still there, can you remove it? Thanks
– hunterrocks777