• 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 Smujukia · Mar 29, 2019 at 04:34 AM · coroutinechildrensetactiveforeachforloop

Load Children One at a Time

Hi, I'd like to load each child of an object one at a time after an event has taken place. So far I'm only able to set the parent game object active but would like another effect, please help!

 void Start ()
 {
     _clickanddragscript._OnMouseUp = false;
     lineParent.SetActive(false);
 }
 
 void Update () {

     if (firstBar.transform.position != Vector3.zero)
     {
         Debug.Log("HasChanged");

         if (_clickanddragscript._OnMouseUp)
         {
             Debug.Log("Mose"); 
             firstBar.transform.DOMove(new Vector3(0, 10.9f), 2, false); 
             GetMyChildren(); 

         }
         
         // Wait 1 second 
         // Move the bar up 
         // Add some indication juice 
         // Load all the children of red lines parent object and wait a little between each load 
     }
     
 }

 void GetMyChildren()
 {
     foreach (Transform child in lineParent.transform)
     {

         for (int i = 0; i < barArray.Length; i++)
         {
         
             lineParent.SetActive(true);
                             // maybe do a coroutine 


         }
         
         
     }

     
 }
Comment

People who like this

0 Show 3
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 xxmariofer · Mar 29, 2019 at 10:55 AM 0
Share

whats the other effect that you want to achieve? you have not telled us

avatar image Smujukia · Mar 29, 2019 at 04:38 PM 0
Share

Oh, it's in the update function already. It's just if player has clicked and let go of mouse, move another object to a position. Now I need help loading all children of a parent one at a time.

avatar image xxmariofer Smujukia · Mar 29, 2019 at 04:43 PM 0
Share
 foreach (Transform child in lineParent.transform){ child.gameObject.SetActive(tue);}

this?

1 Reply

  • Sort: 
avatar image

Answer by David_Rios · Mar 29, 2019 at 07:02 PM

Instead of creating a function for GetMyChildren() you can make a coroutine. It'd be a much cleaner way of activating all your children one-by-one.

This function would activate one of lineParent's children after a certain delay of seconds; I believe this is the effect you want.


Here's an example of a way you could do so with an adjustable time delay:

 IEnumerator GetMyChildren(float delay)
 {
      lineParent.SetActive(true);
 
      foreach (Transform child in lineParent.transform)
      {
            yield return new WaitForSeconds(delay);
 
            child.SetActive(true);
      }
 }



If you wanted a delay of one second, for example, you could call the function in your 'if' statement in the Update function like this: StartCoroutine(GetMyChildren(1f));


Sorry if I misjudged what effect you wanted to achieve, if so, I'll fix my answer, just specify below in a comment.

Comment

People who like this

0 Show 2 · 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 Smujukia · Mar 30, 2019 at 10:17 PM 0
Share

Thanks @BDCakerules! This was helpful. The thing I noticed is that I had to change the child from a transform to a gameobject to set it active, but it didn't work so I did it manually instead and it worked!

IEnumerator NewGetMyChildren() {

     for (int i = 0; i < allMyChildren.Length; i++)
     {
         yield return new WaitForSeconds(delayTime);
         lineParent.SetActive(true);
         allMyChildren[i].SetActive(true);
     }
     
 }
avatar image David_Rios Smujukia · Mar 30, 2019 at 10:31 PM 0
Share

@Smujukia, instead of changing the child you can just access its gameObject component with .gameobject: Ex:


allMyChildren[i].gameObject.SetActive(true);


This will work even if your children are transforms. If my answer helped, please mark it as correct! I'm happy you're getting the effect you want.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

111 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to destroy children one after another in a coroutine, but with a for loop. 1 Answer

Foreach loop trying to grab all children in scene 1 Answer

Why SetActive Game Objects generate too much Garbage in the profiler? 1 Answer

How do you enable/disable a child object individually? C# 1 Answer

Check if card cost (on this instance of clone, or child of parent) is <= Mana, add this card to a list for the Enemy AI to use 1 Answer


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