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 FrancisDuranceau · Feb 03, 2012 at 01:21 AM · childhierarchyforeachgameobject.find

Access one child of object with many duplicate names

Hello,

I'm following a tutorial where we create a cube called "brick" in the hierarchy, then we duplicate it a couple of time, we create an empty object called "row", drag all the "brick" objects under the "row", duplicate the rows a couple of time, create an empty object called wall and put all the rows under it.

So, my Hierarchy looks something like this :

Wall

Row

Brick
Brick

Row

Brick
Brick

Now, I make a script and I would like to access my objects from the script. I can use GameObject.Find("Wall") to find my wall and it works fine. I know I could use GameObject.Find("Wall/Row"); but what when I have multiple item called Row? Is there a way to get something like GameObject.Find("Wall/Row[0]")?

I thought I could use the foreach but when I write this line :

foreach( GameObject rangee in mur )

I get the following error : error CS1579: foreach statement cannot operate on variables of type UnityEngine.GameObject' because it does not contain a definition for GetEnumerator' or is not accessible

What am I doing wrong with the foreach? Plus is there a way to use the GameObject.Find on objects that have the same name?

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

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

Answer by Bicko · Feb 03, 2012 at 11:16 AM

You need to use Transform instead of GameObject in your foreach, because Transform is IEnumerable and GameObject isn't.

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 FrancisDuranceau · Feb 05, 2012 at 04:32 PM 0
Share

Ok, thank you, it's working well if I use the foreach with an IEnumerable component ins$$anonymous$$d of the GameObject. New question that comes to my $$anonymous$$d and I'm asking it even if I haven't searched for it yet, can you get the GameObject that contains the component we get? (I'll search for this right away)

Still, in the first place, is there a way to get something like GameObject.Find("Wall/Row[0]") since all my bricks share the same name? Can I use the FindObject on objects with the same name?

avatar image Bicko · Feb 10, 2012 at 11:06 AM 0
Share

Accessing the parent of the transform is easy: Transform.parent.

For finding objects with the same name, you're best to use Tags. Have a look at this example and the for loop they set up: GameObject.FindGameObjectsWithTag.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

find next(not necessarily immediate next) child of another parent in hierarchy with tag "checkpoint"? 1 Answer

Zooming camera to child objects of .obj model 0 Answers

Get Children of Child and Deactivate/Activate on Demand 1 Answer

Making an array of specific children inside Hierarchy 0 Answers

What is the best way to arrange prefabs in the hierarchy? 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