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).
Both OnDestroy and OnDisable leaves stack traces if you put a Debug.Log in them... sometimes.
I just did a check, and printing a "disabled" in the OnDisable method revealed this information in the console:
disabled!
UnityEngine.Debug:Log(Object)
BastesTestScript2:OnDisable() (at Assets/BastesTestEverything/BastesTestScript2.cs:15)
UnityEngine.Behaviour:set_enabled(Boolean)
BastesTestScript:Foo() (at Assets/BastesTestEverything/BastesTestScript.cs:13)
BastesTestScriptEditor:OnInspectorGUI() (at Assets/BastesTestEverything/BastesTestScript.cs:32)
UnityEditor.DockArea:OnGUI()
If you follow that from the bottom up, you can see that it starts at an editor's OnInspectorGUI (where I'm drawing a button), that calls the Foo method, which disables a different script. Try to add a debug to your OnDisable, click it in the inspector, and see what the stack trace reveals.
Thank you very much.
Unfortunately, I had no luck getting that much information from my Debug.Log inside the OnDisable(). I only get something that looks like the three messages from the top.
Answer by Flum · Feb 19, 2016 at 06:57 PM
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!
Answer by tanoshimi · Feb 19, 2016 at 04:11 PM
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.
Yes, I tried searching for enabled and enable, and going through every reference I had made to the script. I even checked every animation for the odd chance that I had checked the box on the object in an animation somehow.
This was not the case. But thank you very much nonetheless. The problem is "solved". Sort of. See comment below.
Thank you very much for helping out.
Your answer
Follow this Question
Related Questions
In visual studio, attach to unity is not working 1 Answer
[Solved]Cant Find loop that freezes Unity 2 Answers
(Solved) Enabling a script from another script 2 Answers
Help dubugging some beginner code 2 Answers