Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by KaarelKilki · Jan 21, 2021 at 08:52 PM · child objectactive

Set Objects Child to Active Again: SOLVED

Hi! I have this script for activationg my collectables again in the scene. There is a parent name "Collectabes" and it has 15 children in it. Is there a way to shorten the script, so I do not have to write 15 different IEnumerators (one for every child object)?

public class ActivateStrawberriesAgain : MonoBehaviour { public float sec = 14f;

 void Update()
 {
     if (transform.GetChild(0).gameObject.activeInHierarchy == false)
     {
         StartCoroutine(LateCall());
     }
     if (transform.GetChild(1).gameObject.activeInHierarchy == false)
     {
         StartCoroutine(LateCall1());
     }
 }

 IEnumerator LateCall()
 {

     yield return new WaitForSeconds(sec);

     transform.GetChild(0).gameObject.SetActive(true);
 }
 
 IEnumerator LateCall1()
 {

     yield return new WaitForSeconds(sec);

     transform.GetChild(1).gameObject.SetActive(true);
 }

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Jan 21, 2021 at 09:57 PM

Starting a coroutine every frame, for every child (while the child is disabled) is a terrible idea.

Here is my suggestion using a simple list and the Update method:

 private List<float> timers = new List<float>();
 
 private void Update()
 {
     for(int i = 0 ; i < transform.childCount ; ++i)
         ProcessChild(i);
 }
 
 private void ProcessChild(int childIndex)
 {
     GameObject child = transform.GetChild(childIndex).gameObject;
 
     if(childIndex >= timers.Count)
        timers.Add(sec);
 
     if(child.activeInHierarchy == false)
        timers[childIndex] -= Time.deltaTime;
 
     if(timers[childIndex] < 0)
     {
         child.SetActive(true);
         timers[childIndex] = sec;
     }
 }

The code won't support changes in the order of children or removal of child. If you can have this scenario, you can go for a Dictionary<GameObject, float> instead of a List<float>

Comment
Add comment · 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 KaarelKilki · Jan 22, 2021 at 03:24 AM 0
Share

Thank you @Hellium ! It solved my problem :)

avatar image
0

Answer by unity_ek98vnTRplGj8Q · Jan 21, 2021 at 09:24 PM

Something like this should work

 Update(){
     foreach(Transform t in transform){
         if((t!=transform) && !t.gameObject.activeInHierarchy){
             StartCoroutine(LateCall(t));
         }
     }
 }
 
 IEnumerator LateCall(Transform t){
     yield return new WaitForSeconds(sec);
 
     t.gameObject.SetActive(true);
 }

Just use the transform iterator to get each child, then pass it as a parameter to your coroutine to enable it for you.

Comment
Add comment · 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 unity_ek98vnTRplGj8Q · Jan 21, 2021 at 10:02 PM 0
Share

@Hellium has a good point in his answer, as this solution will repeatedly start coroutines while it waits for the objects to enable. If you want to use coroutines I can modify this code to help but his answer is more efficient.

avatar image KaarelKilki · Jan 22, 2021 at 03:27 AM 0
Share

Thanks for answering, I used Hellium's answer and it worked ;)

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

May I know how to reactivate an object that deactivated? 1 Answer

Prefab to save active status of sub Object 1 Answer

Deactivate An Object After Certain Amount of Time 4 Answers

Disable child of clicked object 2 Answers

Active game object before destroying another 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