Why wont the script execute after WaitForSeconds(); ?

Hi! I am trying to move a animation trigger object to a certain postion after 5 seconds of touching another object. I tried following every tutorial I can but I still dont understand why this wont work.

 public GameObject objectToMove;

    private void OnTriggerEnter(Collider other)
    {
            StartCoroutine(Waiting(5));
    }
    IEnumerator Waiting(float delayTime)
    {
        yield return new WaitForSeconds(delayTime);
        objectToMove.transform.position = new Vector3(107.507f, -5.028999f, -11.291f);
    }

Insert Debug.log calls to make sure your code is even running. One at the start of the coroutine, one after the wait, one at the end. Also debug your objectToMove position and make sure it’s targetting what it’s suppose to.

If all your logs print out, then you might have another issue. If not, then it probably means something is turning off this script or cancelling all coroutines before this finishes running.

Hey, so I did this and it seems to not go through the WaitForSeconds line at all, the IEnumerator runs but stops immediately, would you know what’s wrong with it?

Is the object that’s initiating this co-routine being destroyed or disabled after starting said co-routine?

Wow your insanely smart, this was it! Thanks so much!

Probably the most common mistake people do with coroutines:)

Agreed!

For OP, here’s some more coro notes… they’re super-simple and awesome, but not always the best thing:

Coroutines in a nutshell:

https://discussions.unity.com/t/825667/6

https://discussions.unity.com/t/749264/9

Splitting up larger tasks in coroutines:

https://discussions.unity.com/t/840656/2

Coroutines are NOT always an appropriate solution: know when to use them!

https://discussions.unity.com/t/857304/4

https://discussions.unity.com/t/849607/7

Our very own Bunny83 has also provided a Coroutine Crash Course:

https://answers.unity.com/questions/1749615/coroutines-ienumerator-not-working-as-expected.html?childToView=1749714#answer-1749714