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 /
This question was closed Feb 20, 2015 at 05:52 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by loyum · Feb 19, 2015 at 02:45 AM · bugchildcount

Strange code Debugging

Hi everyone! I'am just trying to move some children to an other gameObject, but only half was been transfered correctly!?

Here my code:

        print("old Child Count " + oldGameObject.transform.childCount);
        int i = 0;
        foreach(Transform child in oldGameObject.transform) {
            print("Moving child '" + i + "' to new");
            child.parent = newGameObject.transform;
            i++;
        }
and here the result:
old Child Count 6
Moving child '0' to new
Moving child '1' to new
Moving child '2' to new
And that all...

Somebody can explain that?

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

  • Sort: 
avatar image
5
Best Answer

Answer by meat5000 · Feb 19, 2015 at 02:45 AM

The method you used simply iterates from bottom to top. As you move the objects the count reduces and so as the index increases objects get missed out.

My method iterates from top to bottom using GetChild(index). (Explain how this for loop works with cnt>=0 ??)

 using UnityEngine;
 using System.Collections;
 
 public class CHILDMOVE : MonoBehaviour {
 
     public GameObject oldGameObject;
     public GameObject newGameObject;
 
     void Start () {
         oldGameObject = gameObject;
         NewFunc ();
      }
     
     void NewFunc () {
                 print("old Child Count " + oldGameObject.transform.childCount);
         
                 int blah = oldGameObject.transform.childCount;
                 for(int cnt=blah-1; cnt>=0; cnt--)
                 {
                     print("LATEST old Child Count " + oldGameObject.transform.childCount + " " + cnt);
                     oldGameObject.transform.GetChild(cnt).parent = newGameObject.transform;
                 }
     }
 }
Comment
Add comment · Show 7 · 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 loyum · Feb 19, 2015 at 02:32 PM 0
Share

So easy... Thank's you very much! Why native foreach iterates from bottom to top ins$$anonymous$$d of top to bottom? I don't care anymore... but, I'm curious?

avatar image tanoshimi · Feb 19, 2015 at 02:35 PM 2
Share

"Bottom to top" = "From [0] to [n]" = That's how all default iterators work in every program$$anonymous$$g language - from the first to the last element.

avatar image meat5000 ♦ · Feb 19, 2015 at 02:52 PM 0
Share

Yes I should've added that in. Thanks.

avatar image loyum · Feb 19, 2015 at 08:30 PM 0
Share

Ok, now I understand better, thank you very much!

avatar image meat5000 ♦ · Feb 20, 2015 at 12:04 AM 0
Share

No problem. Click the tick to accept the answer.

Show more comments

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Kinematic Child Object Jumping Above Player 1 Answer

Pressing E three times in order for it to work. 1 Answer

Count how many parents in Hierarchy? 2 Answers

how do i tell how many childs a object has 3 Answers

Transform child out of bounds 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