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
0
Question by mononull · Jul 20, 2012 at 08:07 PM · transformparentchildforeach

using foreach transform child, unity gets stuck

this script locks up or infinite loops when i run the game. the script is attached to a gameobject which has about 30 empty gameobjects as children. the idea is to instantiate the c1 object at each child's position.

 public GameObject c1;
 
 void Start () 
 {
     foreach (Transform child in transform)
     {
         GameObject go =
           (GameObject)Instantiate(c1
           ,child.transform.position
           ,Quaternion.identity);
         go.transform.parent = transform;
     }
 }

HOWEVER, the following runs fine when i remove the foreach child junk...it also works if i remove the nested commands and leave just the foreach loop.....so the loop works on its own...and the instantiating of gameobjects works on its own...but when i combine them like above unity freezes.........what?!?!?!??!!?!?!

 public GameObject c1;
 
 void Start () 
 {
     GameObject go =
        (GameObject)Instantiate(c1
        ,transform.position
        ,Quaternion.identity);
     go.transform.parent = transform;
  
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by mononull · Jul 20, 2012 at 09:00 PM

it could be cuz im adding gameobjects to the parent while im still looping through the children..... so as its looping its creating more children to have to loop through..creating an infinite loop....i'll post back when i can...when i realize whatever obvious mistake im making...i realized this mistake as soon as i stepped outside..but thats how it goes

in response to doireth...all the foreach transform examples ive seen in Unity work this way...but i can give it another look....this script is a component of the object im accessing the children of...so by saying foreach(Transform child in transform) its saying each childs transform which is parented to the transform of the main object

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 Owen-Reynolds · Jul 21, 2012 at 03:34 PM 0
Share

That sounds about right -- you never run out of children, so run forever.

A hack might be to have a dummy empty, to hold them as you create, then reparent later. In the loop, say go.transform.parent=Dummy;. Then run foreach in Dummy and say go.transform.parent=transform;.

NOTE: the less-hackish way is, ins$$anonymous$$d of the Dummy, create LinkedList New$$anonymous$$ids; and use New$$anonymous$$ids.AddLast(go.transform);. Then the same foreach to parent them. But not unless you've used LinkLists before.

avatar image Doireth · Jul 21, 2012 at 07:34 PM 0
Share

Thanks mononull for the info. Shame such things are not documented.

avatar image
0

Answer by Doireth · Jul 20, 2012 at 08:23 PM

foreach works on data structures. Transform (that I know of) is not in any way considered as such.

Also, "child" in the foreach declaration is a variable that you have created and doesn't pertain directly anything in the transform.

Try: (C#)

 Transform[] Children = GetComponentsInChildren<Transform>();
 foreach (Transform child in Children) {
     // whatever
 }

Javascript

 var Children = gameObject.GetComponentsInChildren(Transform);
 for (var child : Transform in Children) {
     // whatever
 }
Comment
Add comment · Show 3 · 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 Owen-Reynolds · Jul 21, 2012 at 03:24 PM 1
Share

Unity has overloaded(?) foreach so that running it over your transform does go through your children. It's the "official" way to touch each child.

I'm assu$$anonymous$$g they didn't just provide a variable like Transform[] transform.children is that children are really stored in some odd way and they didn't want to scare anyone.

avatar image Doireth · Jul 21, 2012 at 07:33 PM 0
Share

Thanks Owen I didn't know that. A much more succinct method.

avatar image Bunny83 · Aug 07, 2012 at 10:18 PM 1
Share

Unity has not overloaded foreach ;) The Transform class implements the IEnumerable interface. That means Transform provides a GetEnumerator function which returns an IEnumerator which can be used to iterate through the direct children of thie transform.

See the foreach documentation ;)

ps: the documentation page of Transform directly shows an example with a foreach loop ;)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unparenting a transform in a foreach loop in the parent transform 3 Answers

Make a simple tree 1 Answer

Separate child rotation 1 Answer

Assign parent object to target 2 Answers

Problem with finding compnent of immidate 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