- Home /
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....
The first time update is called it should call MakeException
The exception will get caught by the try/catch.
After that every frame Update will still be called and it will print a number
What happens instead
The first time update is called it should call MakeException
The exception will get caught by the try/catch.
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);
}
}
}
It might be your LogException. Have you tried commenting that line out?
I'll try it but why could Debug.LogException
cause that. The docs don't mention it as anything special.
Well, you were right. Getting rid of Debug.LogException
fixed it. Debug.LogError
also fails (meaning Update stops being called) where as Debug.Log
works
So lame. Spent the last 6 hours trying to make this work :(
Answer by tanoshimi · Dec 13, 2014 at 08:01 AM
Do you have "Error Pause" enabled?
"When Error Pause is enabled, Debug.LogError() will cause the pause to occur but Debug.Log() will not."
Answer by Kiwasi · Dec 13, 2014 at 08:16 AM
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.
There's no point in having a Debug.LogError or Debug.LogException if all they do is kill your app :( Every other dev environment in existence doesn't stop executing when you call some Log.Error or Log.Execption method. The whole point is to "Log" them. If you want your app to stop after that point you'd either throw the exception again or take some other action.
Changing my $$anonymous$$d again. This is not the problem.
The following code works as expected, spam$$anonymous$$g the console with errors
void Update () {
Debug.LogException (new System.Exception());
}
You're right. It's the Error Pause option which (a) I didn't know existed and (b) shouldn't have arguably triggered since I caught the error, meaning it's not an error :(
Copied your code directly into Unity and it logged an exception as expected, then continued to run update. Not sure what is happening on your system.
Error Pause will trigger as long on LogError or LogException, as well as on a thrown error.