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
3
Question by pickledzebra · May 29, 2010 at 12:19 AM · for-loopparentingunparenting

Buggy child parenting/unparenting? How to avoid For-Loop wackiness?

Generally speaking, I've come across this problem several times - enough to realize that my procedural mind is single-track, but also that Unity behaves poorly in this circumstance.

It appears to be that when looping over the children of one parent object, the child objects cannot be modified without things going wonky.

For example, one cannot loop over the children of one object and set the parent of each to another object. The result is two parents with children assigned in some opaque fashion.

Is there a way to do achieve this without a For-Loop?

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

3 Replies

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

Answer by Molix · May 29, 2010 at 02:37 AM

If you're looping over a transform's children and then changing their parent, you're changing the collection that you're looping through. It is like iterating through any array and deleting elements in the middle; you don't do that, unless you're, say, iterating using an index in reverse order.

So copy the collection first, or iterate by index backwards. Here's an example of copying first (C#):

public void AdoptChildren( Transform currentParent, Transform newParent ) { List<Transform> children = new List<Transform>(); foreach( Transform child in currentParent ) children.Add( child );

 foreach( Transform child in children )
     child.parent = newParent;

}

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 pickledzebra · May 29, 2010 at 12:16 PM 0
Share

Excellent. Thank you!

avatar image
1
Best Answer

Answer by Jessy · May 29, 2010 at 02:07 AM

I reported this bug on September 27th of last year, and it is still open. I haven't actually needed to do this again for awhile, so I haven't picked my own method, but I verified that what Morten Sommer of the Support Team offered is a successful workaround:

using UnityEngine; using UnityEditor; using System.Collections.Generic;

public class CopyPasteChildren : ScriptableObject {

 static List&lt;Transform&gt; transformBuffer;

 [MenuItem("CONTEXT/Transform/Copy Children")]
 static void Copy(MenuCommand mCommand)
 {
     Transform parentTransform = (Transform)mCommand.context;

     if (parentTransform != null)
     {
         transformBuffer = new List&lt;Transform&gt;(parentTransform.GetComponentsInChildren&lt;Transform&gt;());
         transformBuffer.Remove(parentTransform.transform);
     }
 }

 [MenuItem("CONTEXT/Transform/Paste Children")]
 static void Paste(MenuCommand mCommand)
 {
     if (transformBuffer == null || transformBuffer.Count == 0)
         return;

     Transform parentTransform = (Transform)mCommand.context;

     if (parentTransform == null)
         return;

     foreach (Transform tr in transformBuffer)
     {
         tr.parent = parentTransform;
     }
 }

}

Here's the way I used to take care of it:

bool reparenting = true;
while (reparenting)
{
    if (oldParent.transform.childCount > 0)
        foreach (Transform child in oldParent.transform)
            child.parent = newParent.transform;
    else reparenting = false;
}
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 pickledzebra · May 29, 2010 at 12:16 PM 0
Share

Ah, thanks so much!

avatar image
1

Answer by spinaljack · May 29, 2010 at 12:58 AM

You can create an array of the children first and then perform the actions on the array, this should eliminate unwanted behavior.

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 GODLIKE · May 29, 2010 at 01:36 AM 0
Share

LOL that's really SO easy? eheh.

avatar image pickledzebra · May 29, 2010 at 12:15 PM 0
Share

Thanks for the suggestion. :)

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

No one has followed this question yet.

Related Questions

Creating text at the position of the gameobjects in world 0 Answers

Parenting and Unparenting a GameObject that has a Rigidbody 2 Answers

Creating a function that parents player to object and then unparents it 1 Answer

parent player to object when you press a button a set amount of time 1 Answer

How can I de-parent an object from another?? 4 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