- Home /
How to find which script disables my gameobject
Hi guys, im stuck for 2 days now. how do you find which script/gameobject that keeps disabling my gameobject? i have this gameobject that keeps disabled when i run and i couldnt figure which script is causing it. i tried debug.log potential scripts but im lost. is there a way to find which script disables a gameobject?
Answer by _dns_ · May 05, 2018 at 07:34 PM
Hi, it should be possible by implementing a OnDisable() method in one of the scripts that are on this gameobject that gets disabled. This will be called by Unity when another script disables the gameobject. Then, you can use the debugger (visual studio) to set a breakpoint on some line of code inside OnDisable() (just write any dummy code so you can set a breakpoint), attach the debugger to the Editor and press play in the Editor. When the breakpoint is reached, check the callstack window: it will show you all methods that were called until the code that has the breakpoint. Other option is to add a Debug.LogError("") in OnDisable: selecting the message in the console when it pops will show the callstack in the detail/lower part of the console. Using the debugger is better as you can inspect variables content and is a tool .
Just adding a Debug.Log to the OnDisable method should work as well since every log in the console has a stacktrace attached.
Thanks man! how i never thought about OnDisable(). i've found who's the culprit ;)
Quick additional info: if the stacktrace is just the call to OnDisable then it is probably disabled by an animation clip
I'm getting an empty stack trace, I've disabled all animations, and still my script component gets disabled for no apparent reason. Any more ideas?
Thanks. --- EDIT: OK, luckily, quickly after posting this I found out the culprit was an exception the class was throwing. I put that exception there and figured I'd solve it later, but apparently, exceptions in Unity might disable the script that triggers the exception, and the stack trace will be empty.
Your answer
Follow this Question
Related Questions
find next gameobject in hierarchy? 1 Answer
which one of the panels in unity has effect on the size of the project? 0 Answers
Alternatives to GameObject.Find(); 1 Answer
Finding Children question 3 Answers