- Home /
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)
???
@chariot if you want to replace your comment as an answer I'd be happy to up vote it.
Answer by chariot · Feb 05, 2015 at 03:41 PM
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;
}
}
Yes, it is not even an option anymore that I see.
Answer by PrisVas · Feb 05, 2015 at 03:05 PM
Try
transform.GetChild(0).gameObject.SetActive(false);
public bool open = false; // Start is called before the first frame update void Start() {
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("q"))
{
if (open)
{
for (int a = 0; a < transform.childCount; a++)
{
transform.GetChild(a).gameObject.SetActive(false);
}
open = false;
} else
{
for (int a = 0; a < transform.childCount; a++)
{
transform.GetChild(a).gameObject.SetActive(true);
}
open = true;
}
}
}
here i think this will work
For multiple children I would use a loop. Would be something like:
for (int a = 0; a < transform.childCount; a++)
{
transform.GetChild(a).gameObject.SetActive(false);
}
Answer by AmineBMA · Aug 13, 2017 at 06:22 PM
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.
okay was able to solve it, thanks @A$$anonymous$$eB$$anonymous$$A
GameObject Red,Blue,Green;
Red.transform.Find("A5").gameObject.SetActive(false);
Answer by mikembley · Feb 05, 2015 at 03:31 PM
if (selected)
theChild.SetActive(true); // Activate
else
theChild.SetActive(false); //Deactivate
Answer by Digital-Phantom · Feb 05, 2015 at 03:40 PM
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;
}