How to prevent Update from *not* being called

This is NOT the same as other “Update not called” questions. My Update is called but then later it stops being called.

#What should happen I thought…

  1. The first time update is called it should call MakeException
  2. The exception will get caught by the try/catch.
  3. After that every frame Update will still be called and it will print a number

#What happens instead

  1. The first time update is called it should call MakeException
  2. The exception will get caught by the try/catch.
  3. After UPDATE IS NO LONGER CALLED.

I get no notification. I tried adding OnDisable and OnDestroy just to see if Unity was disabling or destroying the script but it’s not. Those don’t get called until I pick stop in unity so the script itself (the MonoBehaviour) is not getting destroyed by the exception. Never the less Update stops being called

How can I call something like the code below and NOT have it stop calling Update?

using UnityEngine;
using System.Collections;

public class UpdateFailure : MonoBehaviour {

    bool once = true;
    int count = 0;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        Debug.Log(count++);
        if (once) {
            once = false;
            MakeException();
        }
    }

    void MakeException() {
        try {
            System.Int32 i = System.Int32.MinValue;
            Debug.Log(System.Math.Abs(i));  // causes an exception
        } catch (System.Exception ex) {
            Debug.LogException(ex);
        }
    }
}

Do you have “Error Pause” enabled?

“When Error Pause is enabled, Debug.LogError() will cause the pause to occur but Debug.Log() will not.”

Edit: Tried it on my own system (see comments) and the following answer does not hold true. Unsure what the problem is.

Original answer:

The problem is the Debug.LogException. This works by passing an exception back to unity, or something similar. This is much the same as if you had written an explicit throw statement.

I’ve had similar issues before. Unity seems to be wired so that if a component throws an exception during Update, it just kills that component and keeps running everything else.

In general you should not expect code to continue running after throwing an exception. Exceptions are generally used to indicate fatal errors. If the server cannot be contacted, but this is okay, then you should log a warning instead.