- Home /
How do I let the script wait a predetermined time?
Firstly, I have really looked up my problem for a long time. And I have also tried a lot of different possibilities. Yet, I still didn't get my code running. Please do not refer to other Threads. I most likely have already read them.
So, for my problem. I tried to let the script wait until an animation has finished playing but somehow the two timestamps I created always show the same Time.
void OnTriggerExit2D(Collider2D other) {
//print(Time.time);
StartCoroutine(WaitForFalling());
//print(Time.time);
}
private IEnumerator WaitForFalling() {
yield return new WaitForSeconds(1);
}
Thank you very much for looking through my code and for your time.
Thank you for answering. Sadly I have no idea what your reply means. But thanks to $$anonymous$$onfuPanda my problem is solved.
Answer by KonfuPanda · Apr 13, 2017 at 01:02 PM
The reason for this behaviour is as follows:
The StartCoroutine in OnTriggerExit just starts a coroutine but does NOT wait for the coroutine to finish! It does not matter that you put a yield in the coroutine itself. The OnTriggerExit is just a normal function, therefore it just continues with the next instruction. In order to achieve what you want I suggest to trigger a coroutine in OnTriggerExit like you did, but to write the whole behaviour in this coroutine, e.g.:
void OnTriggerExit2D(Collider2D other)
{
StartCoroutine(DoSomething(other));
}
IEnumerator DoSomething(Collider2D other)
{
float timeToWait = 1f;
yield return new WaitForSeconds(timeToWait);
// Proceed your code here...
}
Thank you very much. This really helped me, and I understand what I did wrong.
Your answer
Follow this Question
Related Questions
automatic door with coroutine 2 Answers
Having trouble figuring out coroutines in C# 2 Answers
Multiple Cars not working 1 Answer
C# WaitForSeconds isn't waiting for any seconds 2 Answers
Distribute terrain in zones 3 Answers