Hi, I just want to wait the script for 2 seconds before it goes on, but it doesnt work. It doesnt wait for 2 seconds. Is it causing problems because i call it from a Button Script in the Object? It calls the StartTraining() Method.
public float SecondsToWait;
//private void Start()
public IEnumerator WaitingTime()
yield return new WaitForSeconds(SecondsToWait);
public void StartTraining()
SecondsToWait = 2.0f;
Debug.Log("HIT AFTER SECONDS");
Thank you for your time.
I guess you wonder why “HIT AFTER SECONDS” shows up right after you click.
That’s simple enough. When you Start a Coroutine, the Coroutine starts and whatever comes after is executed right away.
Although, within the Coroutine, whatever you put after the yield instruction, will happen after.
Debug.Log("HIT AFTER SECONDS"); right after
yield return new WaitForSeconds(SecondsToWait); within the Coroutine and it’ll work.
Also, a Coroutine can also wait for another Coroutine, but it still have to be of type IEnumerator, so you can’t link it to the onClick event of a button.
Thank you, this was exactly the answer i was looking for.