[SOLVED]Why does my Timer not work? (Called by button Script)

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()
        //{
            //StartCoroutine(WaitingTime());
        //}
    
    
        public IEnumerator WaitingTime()
        {
            //print(Time.time);
            yield return new WaitForSeconds(SecondsToWait);
            //print(Time.time);
        }
        public void StartTraining()
        {
            
            Debug.Log("HIT");
            SecondsToWait = 2.0f;
            StartCoroutine(WaitingTime());
            
            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.

So move 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.