- Home /
How disable a number of components of a gameobject via script?
How can I disable all components from a gameobject (Rigidbody, Scripts, MeshRenderer, etc) less a specific one (MeshCollider), via script.
You can still do it $$anonymous$$anually by unchecking the specific Component you want to Disable from the Inspector.
Answer by jasonlu00 · Sep 05, 2017 at 06:44 PM
"Component" is the Base class for everything attached to GameObjects and "Behaviour" is Component that can be enabled or disabled. So you can do:
Behaviour[] behaviours = gameObject.GetComponentsInChildren<Behaviour>();
foreach(var item in behaviours){
item.enabled = false;
}
and if you are disabling all components, you might as well just disable the gameObject itself.
this.gameObject.SetActive(false);
Sorry, now I have tried it with other components and it doesn't work. Just deactivates some components, for example all the components related to the "$$anonymous$$esh" doesn't deactivate them, why? In addition, these components can be deactivated because if you write this code: GetComponent<$$anonymous$$eshRenderer> ().enabled = false;
they are deactivated.
I would like a script that deactivates all deactivated components related to mesh, such as scripts or audio listeners.
Your answer
![](https://koobas.hobune.stream/wayback/20220612134220im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Disable Component of all child GameObjects 2 Answers
Enable/Disable GameObject Button script 1 Answer
Can I disable automatic creation of materials 2 Answers
Disabling Audio Listener in unity 3d 1 Answer