How do I uncover what is mysteriously disabling a script on start

Hello Answers - and thank you very much for your time!

I have a GameObject “LevelManager” with only one object attached: a C# script named “LevelManager”. At some point in development the script began to disable at game start.

I have read through all scripts in the project to find anything that could be the cause. Nothing. I do disable through code, but most of the time I disable/enable Canvases. I have many references to the script, but none of them use “enable = false;”. The references to the script are used to get and set data in public variables.

I use Animations, and perhaps one of those could have been “recording” a click on the disable-box in the Inspector for that object. I have looked through most of my Animations, but I have found nothing. It seems somewhat unlikely too, as I never lock my Inspector and the Animation windows flip to selected object when clicking on another object in the Hierarchy. So it’s pretty hard to accidentally flip check that box while recording animations.

At this point I have an extra script attached with the sole purpose of re-enabling LevelManager.cs. But even when enabling the script at both Awake and at Start and at Update, it is still disabled at that point of starting up, where all the other scripts are trying to get and set data to it.

LevelManager is enabled as default (before starting the debug / running the game).

My question:

  • Is there some way of backtracking what disables that particular script. Can I attach a script to the LevelManager object, or maybe add a public void that I call from another scripts Update, that “discovers the cause” and gives me a clue of what is disabling it?
  • Can OnDisable() tell me what other object caused the disabling?

(This is the first time I pose a question, and if I have made some mistakes in doing so, I apologize).

Instead of searching for “enable = false;”, I suggest searching through all your script just for “.enabled”

Firstly, the property is called enabled instead of enable, and also you might have written, say:

  • Script.enabled=false;
  • Script.enabled =someVariableWhichIsFalse;
  • Script.enabled =! Script.enabled;

Which would all have the effect of disabling a component, but don’t get returned by your previous search string.

I didn’t succeed in finding a proper way of finding the cause. But the problem is solved.

I think the issue boils down to a public variable being called from another script. It made the usual red error message, about missing the variable. So I checked, and spelled it right.

When the other script couldn’t find the wrongly spelled variable, it disabled the whole LevelManager.cs. (Everything except Update() keeps running, even while disabled). I have no idea why Unity framework chose to just disable it. But spelling the variable name right in the other script seems to have fixed it.

I will close this request now.

Thank you for your kind replies and suggestions!