Coroutine and Update conflict

Hi everyone,

I have a doubt. The documentation states something about this, but it is not clear to me.

I have a code which roughly looks like this:

    void Update()
    {
        //do stuff
    }

    void OnTriggerEnter()
    {
        StartCoroutine(MyCoroutine())
    }

When my AI enters a trigger, the OnTriggerEnter() method runs. This will run MyCoroutine() which has a WaitForSeconds command. I am guessing that my Update() method will continue running regardless of the coroutine command WaitForSeconds(). If this is the case, how do I make sure that the Update() method does not run during the execution of MyCoroutine()? Basically I want MyCoroutine to have full control over my AI until it completely finished running (thus, after the amount of time specified in WaitForSeconds and after the remaining executions which are run in MyCoroutine()).

Thank you in advance.

I would just assign the coroutine to a variable and return from update if it isn’t null or you could just use a bool which you set to true when you start the coroutine and false when it ends.

    private Coroutine _myCoroutine;

    private void Update()
    {
        if (_myCoroutine != null)
            return;
    }

    private void OnTriggerEnter(Collider other)
    {
        _myCoroutine = StartCoroutine(MyCoroutine());
    }

    private IEnumerator MyCoroutine()
    {
        //Do stuff
        yield return null;
        _myCoroutine = null;
    }

Yes, I initially thought of introducing a bool, but I thought maybe Unity had some built in method or something else to perform this trick.

Thank you!