- Home /
Foreach loop trying to grab all children in scene
Hello I have been working to try and figure this out for about 7 hours now(because I don't have to programming experience) and can't seem to get it.
My setup up is I have a gameobject that contains a pool of different objects(not active) that gets instantiated at run. I need to somehow find every object in the pool and randomly activate them at different times. Then move them to different spawn areas so they can do what they need to(i.e. move across the screen(I am working in 2D)). This is my script I have been messing with:
using UnityEngine;
using System.Collections;
public class SpawnPoolItems : MonoBehaviour {
public Transform[] spawnPoints = new Transform[6];
public float spawnMin = 1.0f;
public float spawnMax = 2.0f;
private GameObject[] prefabsToActivate;
private GameObject[] items;
private int randomActive;
// Use this for initialization
void Start ()
{
Spawn();
}
void Spawn()
{
foreach(Transform child in this.transform)
{
child.gameObject.SetActive(true);
/*for(int i = 0; i < items.Length; i++)
{
}
*/
}
//Invoke("Spawn", Random.Range(SpawnMin, SpawnMax));
}
}
I've tried multiple things with for and foreach loops but I keep getting errors and end up back at square one(which is where this script above sits). If anyone has any suggestions on how to do this please help I would be very grateful.
You need to clarify -- 1) are you activating game objects in the scene or are you 2) instantiating prefabs?
Does
foreach(Transform child in this.transform)
{
child.gameObject.SetActive(true);
}
not work for you? It would activate all children of the game object the script is attached to. What do you expect it to do?
I am trying to activate them in the scene, not instantiate(they are instantiated at start as inactive objects). And yes it does work but it activates all of them which I know thats what it's supposed to do, but I want to activate the different sets of game objects(randomly) under that certain parent that the script is attached to at random times. I know I have to use random.Range but I can't figure out how to implement it.
Answer by RudyTheDev · Feb 24, 2014 at 09:30 PM
Here's some code that works over time that should get you started:
public class EnableRandomChildren : MonoBehaviour
{
// Keep a timer since the last time we "spawned" a child
private float timeSinceLastSpawn;
void Update()
{
// Tick down time until next spawn
timeSinceLastSpawn += Time.deltaTime;
// If the 2sec have passed for spawning
if (timeSinceLastSpawn >= 2f)
{
// Schedule the next spawn in 2 sec
timeSinceLastSpawn -= 2f;
// Make a list of all children that are not yet enabled
List<GameObject> availableObjects = new List<GameObject>();
foreach (Transform child in transform)
if (!child.gameObject.activeSelf)
availableObjects.Add(child.gameObject);
// If we found some
if (availableObjects.Count > 0)
{
// Choose a random child from the list
GameObject childToEnable = availableObjects[Random.Range(0, availableObjects.Count)];
// Enable the child
childToEnable.SetActive(true);
// .. and do other things to this child
}
}
}
}
Thank you so much, this is a great answer, now I can see how it works and change it to what I need. I was really stuck there. Thanks again!
Your answer
![](https://koobas.hobune.stream/wayback/20220613134611im_/https://answers.unity.com/themes/thub/images/avi.jpg)