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 TheGeneralBenLee · Apr 07, 2021 at 12:27 PM · gameobjectbeginnersetactiveparent-childparent and child

2020 Unity. How do I set children of a parent object to be active?

I am making a game where the player can build structures. The buildings for the sake of my prototype have 5 stages. I am storing all stages in a custom ConditionsMet() function. it increases the stage number (Int) by 1, then using Debug.Log i confirm which stage the parent is in. The parent object is called BuildingFoundation, and the script is attached to this object. Attached to the object are 5 groups of objects (In this instance they each represent 5 walls, for each object group, at different heights) , which I have set as inactive for now. I want to, at each stage, set each group of objects to be active. stage 1 walls are active at stage 1, stage 2 deactivates stage one walls and activates stage 2 walls, stage 3 onwards copies the same function as stage 2. I already have the if statements set up, I just cant figure out how to set child group objects to be active without cluttering up my code (I am still learning C# at a beginner level) I tried gameobject.setactive(true) but this only works for what the script is attached to. Thank you in advance everyone, I am keen to further my learning and this would really help me understand more about parent child connections etc.

I want to add that i want the foundation object (the parent) to remain active throughout. i know that this doesnt need code cause its just an object. the foundation object is basically what i use to hold the script and locate the object in world.

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 HellsHand · Apr 07, 2021 at 12:56 PM

You could use a for loop to set each of the children active.

 for (int i = 0; i < transform.childCount; i++)
 {
     transform.GetChild(i).gameObject.SetActive(true);
 }

If they are nested children, you would need to activate the parent object first transform.GetChild(#).gameObject.SetActive(true); you would use transform.GetChild(#).childCount for the loops initialization and then in the loop would be transform.GetChild(#).GetChild(i).gameObject.SetActive(true); where # is the child's order in the hierarchy starting at 0.

 transform.GetChild(#).gameObject.SetActive(true);
 for (int i = 0; i < transform.GetChild(#).childCount; i++)
 {
     transform.GetChild(#).GetChild(i).gameObject.SetActive(true);
 }

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

Answer by EliasMiettinen · Apr 07, 2021 at 01:47 PM

Also you can use a foreach loop as well.

 foreach(Transform t in transform) 
 {
     t.gameObject.SetActive(true);
 }
Comment
Add comment · 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

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

189 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 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

Is what a parent gameobject can consult or modify components attributes of it desactivated gameobject children 0 Answers

How to reactivate items ? 4 Answers

SetActive Button from another scene isn't working in build 0 Answers

GameObject.setActive(true) not working despite children and parents are active 1 Answer

Parent and Child looking at mouse 0 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