• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by serenefox · Feb 24, 2014 at 08:20 PM · childchildrensetactiveforeachfor loop

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.

Comment

People who like this

0 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RudyTheDev · Feb 24, 2014 at 08:24 PM 1
Share

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?

avatar image serenefox · Feb 24, 2014 at 08:52 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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
             }
         }
     }
 }
Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image serenefox · Feb 24, 2014 at 11:17 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Grabbing all Children and components of those children 1 Answer

Switch for children not working 1 Answer

Get Children of Child and Deactivate/Activate on Demand 1 Answer

Load Children One at a Time 1 Answer

Set the Sprite for a child of a prefab before intstantiation. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges