Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Stavros-Dimou · Nov 21, 2017 at 02:11 PM · getcomponentsinchildren

How to NOT get components from grandchildren ?

Hello everybody. I'm working on a load/save sub-system for a game, and I want the saves to save the transform data of various items that are in the scene. So here's what I've done: I added all items that are in the scene in a single gameObject named 'Items', and I use GetComponentsInChildren<>() to get the transforms. So when a save file is loaded, the script finds the 'Items' gameObject, runs GetComponentsInChildren<>(), and if the name of the gameObject in the save file is the same as the name of the gameObject of a particular transform in the array that is returned by GetComponentsInChildren<>(), it changes the transform data to the ones of the save file.

There is a problem though. You see, the items are gameObjects that have their own children, often things like LOD meshes, or other meshes that become visible under certain circumstances etc. And GetComponentsInChildren<>() not only returns the transforms of the children, but also the grandchildren. And that is a problem. Because a child gameObject of an Item object may contain a gameObject with the exact same name as the child gameObject of another Item. So if you have like some instances of an Item in the scene, the meshes of all move to one single position, making all instances but one to be 'invisible'.

So, is there an easy way to get all the components of children ONLY, and not the grandchildren ? Please tell me there is one.

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

1 Reply

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

Answer by Hellium · Nov 21, 2017 at 02:22 PM

Try the following solution (not tested)

Create a new C# file called GameObjectExtensions.cs and put the following code

 using UnityEngine;
 using System.Collections.Generic;
 
 public static class GameObjectExtensions
 {
     public static T[] GetComponentsInDirectChildren<T>( this GameObject gameObject ) where T : Component
     {
         List<T> components = new List<T>();
         for( int i = 0 ; i < gameObject.transform.childCount ; ++i )
         {
             T component = gameObject.transform.GetChild( i ).GetComponent<T>();
             if( component != null )
                 components.Add( component ) ;
         }
         
         return components.ToArray();
     }
 }

Then, in your code :

 MyScript[] scripts = gameObject.GetComponentsInDirectChildren<MyScript>();


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

72 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

Related Questions

grab weapon script problem 1 Answer

Storage of children references efficiency 1 Answer

GetComponentsInChildren not working 0 Answers

trouble getting all children into an array. 1 Answer

short script error mesige 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