Set Objects Child to Active/Inactive(Solved)

Part of my script allowed for a gameObject to be set active/inactive depending if it was selected(or not)

if(selected)
	{
		
		// Activate/Deactivate the game object/child.
		gameObject.SetActive(true);
    }

Things have changed a little now and that gameObject has a single child that I now want to make active/inactive. There seems to be so many conflicting ideas about how to do this its confusing.

Whats the simplest way to access the child object and deactivate it. (The parent object is always active)

???

I actually came up with a solution based on your suggestion. private var lightRing : Projector; function Start () { lightRing = GetComponentInChildren(Projector); } if(selected) { lightRing.enabled = true; }

@chariot if you want to replace your comment as an answer I'd be happy to up vote it.

6 Answers

6

Try

transform.GetChild(0).gameObject.SetActive(false);

didn't work

worked for me, thnx PrisVas !

WOW MAN! This work for me, thanks!

Thank you! Worked like a charm and so simple!

OMG! thanks alot. this worked for me also :D

My answer, glad that it helps you

private var childObj : Transform;
 
 function Start () {
   childObj = transform.Find("Child Name");
 }
 
 function Update () {
   if (selected){
     childObj.active = true;
   }
 }

isn't .active deprecated?

parentGameObject.transform.GetChild(0).gameObject.SetActive(false);

I have 3 GameObjects namely red, blue and green, and each has children objects named A1, A2, A3....A100. (all 3 objects have children of the same name). How do I disable one specific object? Say A5 of Red, and A7 of Blue.

I have 3 GameObjects namely red, blue and green, and each has children objects named A1, A2, A3…A100. (all 3 objects have children of the same name).

How do I disable one specific object? Say A5 of Red, and A7 of Blue.

I actually came up with a solution based on your suggestion.

private var lightRing : Projector;
    
    function Start ()
    {
        lightRing = GetComponentInChildren(Projector);
    }
    
    if(selected)
    	{
    		lightRing.enabled = true;
    	}

if (selected)
theChild.SetActive(true); // Activate
else
theChild.SetActive(false); //Deactivate