- Home /
Question by
acclogin71 · Sep 19, 2018 at 05:44 PM ·
transformchildrenindex
how to find, not a specific child (GetChild(0)), but a range of child objects (GetChild(0 to 4))
there are 9 child gameobjects to a parent.
only one child is active at a time, rest are disabled.
I want to do something like
if current active childindex is less than 4 then setactive(false) all the children and setactive(true) childindex(0)
if current active childindex is between 4 - 9 then setactive(false) all the children and setactive(true) childindex(4)
currently I have if statement for all 9 childrens like so
if (transform.GetChild(1).gameObject.activeInHierarchy == true)
{
transform.GetChild(1).gameObject.SetActive(false);
transform.GetChild(0).gameObject.SetActive(true);
}
if (transform.GetChild(2).gameObject.activeInHierarchy == true)
{
transform.GetChild(2).gameObject.SetActive(false);
transform.GetChild(0).gameObject.SetActive(true);
}
and so on. but I know there must be a better way to deal with this. so please help. thanks in advance.
Comment
Best Answer
Answer by Casiell · Sep 19, 2018 at 05:55 PM
Do a for loop?
for(int i = 0; i <= 9; i++)
{
if(i < 4 && transform.GetChild(i).gameObject.activeInHierarchy)
{
transform.GetChild(i).gameObject.SetActive(false);
transform.GetChild(0).gameObject.SetActive(true);
}
else if(i >= 4 && transform.GetChild(i).gameObject.activeInHierarchy)
{
transform.GetChild(i).gameObject.SetActive(false);
transform.GetChild(4).gameObject.SetActive(true);
}
}