'Find which script is activating the object
There is a disabled gameobject in the scene. When i started the game that gameobject becomes enabled somehow. How do i find which script enables that object?. Is there a trick to find ? My code is too spaghetti even i dont understand.
Solution 1:[1]
First of all
My code is too spaghetti even i dont understand.
well ... this is bad so clean this up first before doing anything else ^^
Simplest way I can imagine would be a simple debugger component/method call:
public class WhoIsEnablingThis : MonoBehaviour
{
private void OnEnable()
{
Debug.Log("I just got enabled!", this);
}
}
and put this on the according GameObject
.
This causes a log message being printed to the console including a callstack where you might already be able to see from which method exactly the object was enabled.
Example:
I just got enabled!
UnityEngine.Debug:Log(Object, Object)
WhoIsEnablingThis:OnEnable() (at Assets/WhoIsEnablingThis.cs:10)
UnityEngine.GameObject:SetActive(Boolean)
Enabler:Start() (at Assets/Enabler.cs:12)
in my example script Enabler.cs
indeed I have the line 12 in Start
:
private void Start()
{
example.SetActive(true);
}
The next step if the console is not enough would then be to set a breakpoint to the Debug.Log
line and debug your code line by line e.g. with VisualStudio.
Once this line's breakpoint is hit in VisualStudio simply enable the callstack view and you can directly go through all methods leading to this enabling.
Once you reached the component which is originally causing the call (in my case the Enabler
class) you can look at the name
field of MonoBehaviour
classes so you will know exactly which GameObject
this component is attached to. If you have multiple with the same name at least you can still bubble up through the transform.parent
properties until you have the information you need.
Solution 2:[2]
For those looking at derHugo's response above who can't see the CallStack info in the Console, you need to enable it via the options / hamburger menu for the Console > Stack Trace Logging > Log > ScriptOnly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | jasoben |