- Home /
Disabling GameObject doesn't disable components
Simple question. I'm doing this on Unity4:
conversationCamera.gameObject.SetActive(false);
Where conversationCamera is a custom MonoBehaviour.
This code disables the gameObject that contains said conversationCamera, however all it's other components remains active (still calling update, AudioListener is still working, etc).
I wish to disable all of that GameObject's components. Am I doing something wrong?
Scripts don't run on inactive objects, so I'd guess that it's being reactivated elsewhere, or possibly not actually set to inactive somehow.
$$anonymous$$aybe something like this? ! Dont use this code.
Component[] allComponents = gameObject.GetComponents<Component>();
foreach(Component component in allComponents)
{
var currentComponent = component.GetType();
currentComponent.disable(); //idk... Debug.Log(currentComponent.GetType());
}
As Eric mentioned, the components would not be running if the object was actually disabled. Also a component doesn't have a disable() method - it would be component.enabled = false - but this is not necessary.
To @whydoidoit :
Now I'm getting curious... How excactly do you disable a Component variable? .enable = false; doesnt work with Component...
You're right, it's Behaviour that has that I believe. There's nothing to "enable" in Component, "enable" means allow coroutines and Update functions (collision etc) to execute.
Your answer
Follow this Question
Related Questions
'UnityEngine.Component.active' is obsolete. 4 Answers
Do you tend to have scripts to mark prefabs 1 Answer
Deactivate an object - and all scripts in that object deactivated? 1 Answer
Disable/Enable Script or Add/Remove? 1 Answer
How to make an open and close box?,How to make open and close a box? 0 Answers