Search Unity

Disabling all Components except for one with a certain type

Discussion in 'Scripting' started by hazem_inspectar, Jan 17, 2020.

  1. hazem_inspectar

    hazem_inspectar

    Joined:
    Jan 10, 2020
    Posts:
    7
    Hello,

    I am basically trying to disable all the game objects under the parent except for all inputFields. I have the following script but it doesn't seem to work.

    Code (CSharp):
    1.  
    2.             foreach (Behaviour childCompnent in MarkupTool.GetComponentsInChildren<Behaviour>())
    3.             {
    4.  
    5.  
    6.                 if (childCompnent is InputField) {
    7.  
    8.                     childCompnent.enabled = true;
    9.                 }
    10.  
    11.                 else
    12.                 {
    13.                     childCompnent.enabled = false;
    14.                 }
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I think you might be confusing Components and GameObjects. Each GameObject (in the scene list) can have multiple Components attached to it (in the Inspector when you click that GameObject). You can enable/disable entire GameObjects (the checkbox at the very top of the inspector) or enable/disable Components individually on a GameObject (the checkbox next to each component in the inspector). Your script would disable all the components on all the GameObjects except for the InputFields, but leave the actual GameObjects active. If you want to disable all GameObjects except the ones that have an InputField component attached you can do this:

    Code (csharp):
    1. foreach(var child in MarkupTool.GetComponentsInChildren<Transform>()) {
    2.   if (child.GetComponent<InputField>() != null)
    3.     child.gameObject.SetActive(true);
    4.   else
    5.     child.gameObject.SetActive(false);
    6. }
     
    Last edited: Jan 17, 2020
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    To clarify, do you want to disable the Gameobject or the components in the children?

    If all you want to do is disable the GameObject, just loop through the children, use GetComponent to check if it has a Inputfield and nvm... @makeshiftwings beat me to it. :)
     
    makeshiftwings likes this.
  4. hazem_inspectar

    hazem_inspectar

    Joined:
    Jan 10, 2020
    Posts:
    7
    @Brathnann yes I am sorry I didn't make it clear. I attached a screenshot of the gameObject I have. I need to deactivate all what's under except for the inputField clones. I tried @makeshiftwings code but doesn't seem to work
     

    Attached Files:

  5. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,330
    Code (CSharp):
    1. for(int i = 0; i < MarkupTool.transform.childCount; i++)
    2. {
    3.     var child = MarkupTool.transform.GetChild(i);
    4.     bool isInputField = child.GetComponent<InputField>() != null;
    5.     child.gameObject.SetActive(isInputField);
    6. }
    @makeshiftwings 's code probably doesn't work for you because it uses GetComponentsInChildren, which causes the nested child gameobjects under the input fields to also get set inactive, while I'm guessing that you only want to set the direct children under MarkupTool inactive.
     
    makeshiftwings likes this.
  6. hazem_inspectar

    hazem_inspectar

    Joined:
    Jan 10, 2020
    Posts:
    7
    thanks @SisusCo I got it to work finally, I just had to push the inputField clones to be under the Parent game object directly.
     
    SisusCo likes this.