Activate all child objects in a Parent object
Hi guys,
is there a way for me to activate all child objects within a parent object at once?
For example, i have an empty parent object, in this empty parent object i have coins in certain positions.
Once a coin is picked up, that one child coin within the parent is set to SetActive(false) while the rest are still active. Once these coins go past a certain x position, the parent object is SetActive(false).
When this parent is called again from my object pooler, it is SetActive(true). HOWEVER, previously, that one coin that was picked up was set to false, so when the parent is set to true, that one coin is still set to false.
How can i enable all previously inactive child objects?
I hope this makes sense,
Edit: it looks like using the obsolete method SetActiveRecursively(true); enables all the children in the parent object? how come setactive(true) does not?
Thanks
Answer by ScottKane · May 14, 2020 at 11:16 PM
A script on the parent object containing this will set all children of the object to active when the parent is enabled.
private void OnEnable()
{
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(true);
}
}
Answer by Piro1503 · May 14, 2020 at 11:01 PM
First create a public "GameOjectArray", then in an example function "Start ()" place a For and follow this Script.
public class Example : MonoBehaviour
{
public GameObject[numCoins] coins;
public GameObject[numHealth] health;
public void OnEnable()
{
for (int i = 0; i < numCoins; i++)
{
coins[i].SetActive(true);
}
for (int x = 0; x < numHealth; x++)
{
health[x].SetActive(true);
}
}
}
For me it worked very well
Your answer
Follow this Question
Related Questions
How can I move a gameobjects box Collider with another asset? 0 Answers
ChildsParentObject Function 0 Answers
SetActive between gameobjects 1 Answer
GameObject SetActive not reactivating 5 Answers