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 NyanPixel · May 02, 2016 at 09:43 PM · c#transformlistchildrenrecursion

Trying to Recreate Transform Child Heirarchy to a List

Hi,

I have spent most of the day trying to work out how to 'copy' the structure/hierarchy of a transform and all it's children, grandchildren, etc, but to a different class (structure example below).

I have tried recursion but can't maintain the order. A 'flat' list for example is easy to get but I just can't get it how I need. Here is an example of the simple class structure I am experimenting with:

         [Serializable]
         public class BasicCopy
         {
             public string Name;
             public List<Transform> Transforms;
         }
     
         public List<BasicCopy> Test;
 
         public Transform Target;
 
         void Start()
         {
             // Iterate through Target and ALL it's children and generate a matching hierarchy/structure using the BasicCopy class
         }

The whole process keeps tying me in knots, and is now driving me insane lol! Any help will be much appreciated.

EDIT: I may have worded this badly. For clarity, I am looking to specify my 'Target' transform then recursively find all child transforms, as I find them, they are added to my BasicCopy class, maintaining the same hierarchy/structure as the children.

Comment
Add comment · 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 Cherno · May 03, 2016 at 06:41 AM 1
Share

How about giving each object a unique ID and then just keeping track of three things:

  1. the GameObject

  2. It's ID

  3. It's parent's ID, if any.

avatar image NyanPixel Cherno · May 03, 2016 at 08:04 AM 1
Share

Hi Cherno,

Thanks, but this does not provide me with the matching structure, in the exact way specified above.

As mentioned, I can already generate a 'flat' version of the data. I need the parent / child/ grandchild structure to exactly match that of the target transform.

avatar image Glurth NyanPixel · May 04, 2016 at 04:10 PM 0
Share

It requires processing, but this DOES in-fact provide you with the structure information. Additionally, all those things Cherno listed can be stored in a flat array, and thus, always be serialized properly. You can create and implement a serialize() and deserialize() pair of functions to convert from the flat array (for serialized storage), to a tree structure (for run-time usage).

2 Replies

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

Answer by rober-psious · May 03, 2016 at 08:55 AM

Here you have an example:

 [SerializeField]
 public class BasicCopy
 {
     public string Name;
     public List<Transform> Transforms;
 }
 
 public List<BasicCopy> Test;
 
 public Transform Target;
 
 private void Start()
 {
     Test = new List<BasicCopy>();
     Test.Add(GetBasicCopy(Target));
 }
 
 private BasicCopy GetBasicCopy(Transform currentTarget)
 {
     BasicCopy result = new BasicCopy();
     result.Name = currentTarget.name;
     result.Transforms = new List<Transform>();
 
     Transform[] childs = currentTarget.GetComponentsInChildren<Transform>();
     foreach (Transform current in childs)
         if (current.parent != null && current.parent.GetInstanceID() == currentTarget.GetInstanceID())
         {
             result.Transforms.Add(current);
             Test.Add(GetBasicCopy(current));
         }
 
     return result;
 }

Comment
Add comment · Show 4 · 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 NyanPixel · May 03, 2016 at 12:19 PM 0
Share

Hi,

That has really helped me get started. Unfortunately my example class was wrong as it would only go so far, and would only get the first level. However after your help I am getting close (Thanks!).

So this code I have worked on (I have modified the class) gets me the first children of the transform, but fails to get the rest.

Here is where I am currently at:

             [Serializable]
             public class BasicCopy
             {
                 public string Name;
                 public Transform $$anonymous$$odel;
                 public List<BasicCopy> Children;
             }
         
             public Transform Target;
         
             public List<BasicCopy> Test;
             
             // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         
             private void Start()
             {
                 Test = new List<BasicCopy>();
                 Test.Add(GetBasicCopy(Target));
             }
         
             // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         
             private BasicCopy GetBasicCopy(Transform currentTarget)
             {
                 BasicCopy result = new BasicCopy();
                 result.Name = currentTarget.name;
                 result.Children = new List<BasicCopy>();
         
         
                 Transform[] childs = currentTarget.GetComponentsInChildren<Transform>();
                 List<BasicCopy> convertedChilds = new List<BasicCopy>();
         
                 foreach (Transform t in childs)
                 {
                     BasicCopy newChild = new BasicCopy();
                     newChild.$$anonymous$$odel = t;
                     newChild.Name = t.name;
                     convertedChilds.Add(newChild);
                 }
         
                 foreach (BasicCopy current in convertedChilds)
                 {
                     if (current.$$anonymous$$odel.parent != null && current.$$anonymous$$odel.parent.GetInstanceID() == currentTarget.GetInstanceID())
                     {
                         result.Children.Add(current);
                     }
                 }
         
                 return result;
             }
     

Any ideas where I am going wrong? It feels like I am really close. Thanks again for the help so far! Here is an example of the result so far:

alt text

P.S. I know my new class will generate a "Serialization depth limit exceeded at 'BasicCopy'. There may be an object composition cycle in one or more of your serialized classes." error. I am serializing it just to get quick access to results. It compiles fine.

capture.png (35.6 kB)
avatar image rober-psious NyanPixel · May 03, 2016 at 12:55 PM 1
Share

You are using serialization system which has an unity problem. This http://answers.unity3d.com/answers/577519/view.html can help you, but I don't believe you need this.

avatar image NyanPixel rober-psious · May 04, 2016 at 08:29 PM 0
Share

Thanks! I did not know this!

avatar image NyanPixel · May 04, 2016 at 10:27 PM 0
Share

This gave me enough in the end, and I learnt lots from this. $$anonymous$$arking as answered. Thank you!

avatar image
1

Answer by meat5000 · May 03, 2016 at 08:39 AM

Try using GetChild and incrementing the index

http://docs.unity3d.com/ScriptReference/Transform.GetChild.html

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

10 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

Related Questions

Manipulating booleans from parent to child (C#) 2 Answers

Multiple Cars not working 1 Answer

Dynamic Enemy Transform List 1 Answer

How to move the parent object to a child of one of its children? 2 Answers

How to move the parent object to a child of one of its children? 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