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 Pathojen · Aug 04, 2020 at 07:28 AM · childbooleanchildrenactivatedeactivate

Most effective way to get deactivated children?

Hello.

I have a player object with all its potential armor pieces on it and deactivated. I also plan to add its potential weapons. Thing is, I didn't find much on this topic that was updated and that worked on multiple children. There are too many children to hardcode bools for everything. Is there a more effective way to deactivate them all and only activate the one(s) needed?

Comment
Add comment · Show 1
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 · Aug 04, 2020 at 08:53 AM 0
Share

if you want to specify which child gets activated/desactivated one by one you will need to set it up manually there is no other solution, but if you dont post your code we cant help you

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RaptorRush · Aug 09, 2020 at 08:39 PM

The transform component holds references to all his children, so you can search them by: Index: (Transform.GetChild())

 Transform child = yourTransform.GetChild(i);

Name: (Transform.Find())

 Transform child = yourTransform.Find("ObjectName");



You can access to the transform of an object:

 Transform yourTransform = yourGameObject.transform;

You can get the children count in an object:

 int childCount = yourTransform.childCount;

With this you can loop trough all the children of an object:

 for (int currentChildI = 0; currentChildI < transform.childCount; currentChildI++) {
      Transform currentChild = transform.GetChild(currentChildI);
 }



You can also write your own extensions methods to make your life easier, for example:

 using UnityEngine;
 
 public class Example : MonoBehaviour {
     public Transform swords, hats;//Your containers
     private void UsageExamples() {
         swords.SetStateOfAllChildren(false);//Would disable all swords
         hats.SetStateOfAllChildren(true);//Would enable all hats
         //
         swords.SetStateOfChild(true, "FireSword");///Would enable the object named "FireSword"
         hats.SetStateOfChild(false, 3);///Would disable the object in the index 3 (Zero-based numbering)
     }
 }
 public static class Extensions {
     public static void SetStateOfAllChildren(this Transform transform, bool newState) { for (int currentChildIndex = 0; currentChildIndex < transform.childCount; currentChildIndex++) transform.GetChild(currentChildIndex).SetStateOfGameObject(newState); }
     public static void SetStateOfChild(this Transform transform, bool newState, string childName) => transform.Find(childName).SetStateOfGameObject(newState);
     public static void SetStateOfChild(this Transform transform, bool newState, int childIndex) => transform.GetChild(childIndex).SetStateOfGameObject(newState);
     private static void SetStateOfGameObject(this Transform transform, bool newState) => transform.gameObject.SetActive(newState);
 }


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

138 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

Related Questions

How to make a Boolean unchangeable 3 Answers

Deactivating and Activating a group and all it's children? 1 Answer

C# Find component InChildren 3 Answers

How to activate the child of an object without declaring a public transform? 1 Answer

Deactivating a Child in C# 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