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 /
  • Help Room /
avatar image
0
Question by Baity · Aug 06, 2020 at 01:07 PM · bugscripting beginnerforeach

Foreach skipping game objects.

Been fighting with some code all day, so I created a test example, and Foreach definately appears to be skipping items! I'm new to Unity and C# so could be something I'm doing, but I can't tell what!

I have 2 Game objects (Container1 and container2)

I placed 5 Game objects into container1 (GameObject1,Gameobject2,...)

I added the following script to container 1:

 void Start()
     {
         GameObject newParent = GameObject.Find("Container2");
         foreach (Transform child in transform)
         {
             child.parent = newParent.transform;   
         }
     }

And after running this Object1,object3, and object5 are moved to Container2

Object2 and Object4 remain in container1

Comment
Add comment · Show 5
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 06, 2020 at 01:12 PM 0
Share

the code is fine, you will need some extra debugging to check if the foreach is really only looping 3 times also use a loop next to it and test if with the for loop it does work

avatar image Baity xxmariofer · Aug 06, 2020 at 01:23 PM 0
Share

Hi xxmariofer, could you give me a pointer as to what would help, like I say I'm quite new to unity and to c#!

avatar image xxmariofer Baity · Aug 06, 2020 at 01:27 PM 0
Share

add all this debugging

 for(int i = 0; i < transform.childCount; i++)
 {
    Debug.Log("Number of childs: " + i);
 }
 
 foreach (Transform child in transform)
 {
     Debug.Log(child);
 }

avatar image Baity xxmariofer · Aug 06, 2020 at 01:26 PM 0
Share

If I add a Debug.log line within the loop it logs 3 time. Again suggesting it is the loop that is skipping (as opposed to the parent change failing)

avatar image xxmariofer Baity · Aug 06, 2020 at 08:00 PM 0
Share

It os hard to tell i cant reproduce the issue fill free to upload a project but i bet the issue is somewhere else

1 Reply

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

Answer by Hellium · Aug 06, 2020 at 08:40 PM

You are modifying the collection while looping through it. Either store the children in an array or loop in from last to first

  void Start()
  {
      GameObject newParent = GameObject.Find("Container2");
      
      for(int i = transform.childCount - 1 ; i >= 0 ; --i)
      {
          transform.GetChild(i).parent = newParent.transform;   
      }
  }
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

227 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 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

Player keeps teleporting to a set Y coordinate when moving 0 Answers

Taking continuously damage on a surface 0 Answers

Foreach freezing computer 1 Answer

[Help] I don't know how to create a stacking system for my inventory! 3 Answers

One script stopped working after update 0 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