Search Unity

Disable all scripts of an object C#

Discussion in 'Scripting' started by aagrlp640, Dec 18, 2014.

  1. aagrlp640

    aagrlp640

    Joined:
    Aug 9, 2013
    Posts:
    16
    Hi I want to disable all scripts that are set to gamobject childs, for example with the next script i disable all Mesh Render, that are inside a gameobject, the question is how can i dasable all scripts

    foreach (GameObject go in GameObject.FindGameObjectsWithTag ("ParentTag")) {
    Renderer[] rs = go.GetComponentsInChildren<Renderer>();
    foreach(Renderer r in rs)
    r.enabled = false;
    }

    Thanks
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Replace the word Renderer with MonoBehaviour
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Renderer isn't derived from MonoBehaviour, so you'll have to use an additional method. Try this:
    Code (csharp):
    1. foreach (GameObject go in GameObject.FindGameObjectsWithTag ("ParentTag")) {
    2.     foreach (Component component in go.GetComponentsInChildren(typeof(Component))) {
    3.         SetComponentEnabled(component, false);
    4.     }
    5. }
    6.  
    7. public void SetComponentEnabled(Component component, bool value) {
    8.   if (component == null) return;
    9.   if (component is Renderer) {
    10.     (component as Renderer).enabled = value;
    11.   } else if (component is Collider) {
    12.     (component as Collider).enabled = value;
    13.   } else if (component is Animation) {
    14.     (component as Animation).enabled = value;
    15.   } else if (component is Animator) {
    16.     (component as Animator).enabled = value;
    17.   } else if (component is AudioSource) {
    18.     (component as AudioSource).enabled = value;
    19.   } else if (component is MonoBehaviour) {
    20.     (component as MonoBehaviour).enabled = value;
    21.   } else {
    22.     Debug.Log("Don't know how to enable " + component.GetType().Name);
    23.   }
    24. }
    The issue is that each of those component types (Renderer, Collider, etc.) define their own, separate enabled properties, so they need to be handled separately.
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I believe he was just using Renderer as an example. If you just want to disable user scripts, which would all be derived from MonoBehaviour, that's the base class to look for.

    That being said, Animation, Animator, AudioSource and MonoBehaviour all derive from Behaviour, so your function could be refactored as follows:

    Code (CSharp):
    1. void SetChildComponentsEnabled(bool value) {
    2.  
    3.     var parentObjects = GameObject.FindGameObjectsWithTag ("ParentTag");
    4.  
    5.     foreach (var gameObject in parentObjects) {
    6.  
    7.         var components = gameObject.GetComponentsInChildren<Component>();
    8.    
    9.         foreach(var component in components) {
    10.             var collider = component as Collider;
    11.             if (collider) collider.enabled = value;
    12.  
    13.             var renderer = component as Renderer;
    14.             if (renderer) renderer.enabled = value;
    15.  
    16.             var behaviour = component as Behaviour;
    17.             if (behaviour) behaviour.enabled = value;
    18.         }
    19.     }
    20. }
     
    Last edited: Dec 18, 2014
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Thanks! I'm sure I also missed some other non-Behaviour components, too. It's probably worthwhile for the OP to check what components are on the scene's GameObjects and check their inheritance hierarchy.