- Home /
setActive(false) does not fire onDisable?
If I use setActive(true) on a gameObject unity will call: onEnable. But when I am using setActive(false) on the same gameObject Unity would not call onDisable. The reference says that onEnable is called when the OBJECT becomes enabled and active. OnDisable is called when the BEHAVIOUR becomes disabled () or inactive. Is there any other callback that is called when setActive(false) on a gameObject is used?
Answer by Bunny83 · Feb 03, 2014 at 01:10 PM
I can't really reproduce your result. Are you sure you have your method called "OnDisable" (note: capital first letter!)?
This C# snippet works as expected:
void OnDisable()
{
Debug.Log("DISABLED");
}
void OnGUI()
{
if (GUILayout.Button("turn off"))
{
Debug.Log("Going to disable this GO");
gameObject.SetActive(false);
Debug.Log("Disabling completed");
}
}
I got the 3 logs in this order:
Going to disable this GO
DISABLED
Disabling completed
Are you sure the script was enabled and the GO is active then you call SetActive(false) ?
edit
ps: no, there's no other callback beside OnDisable
$$anonymous$$y fault. I did used onDisable not OnDisable. Stupid. Thanks a lot for your hint!
Never$$anonymous$$d ;) I also have trouble from time to time. C# uses the "Pascal"-Case (UpperCamelCase) whereas a lot other languages prefer traditional lowerCamelCase. When i switch between C# C++ or LUA i always get in trouble with the method names. In the case of Unity it's evil since you don't get any errors ;)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I call an animation event on a deactivated gameobject in unity3d c# 0 Answers
Do C# events work in unity? 1 Answer
Distribute terrain in zones 3 Answers