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 Essential · May 22, 2012 at 02:48 AM · gameobjectarraycomponentfind

Search for specific entity in an array?

I created an array to store a set of windows in a house, and it was populated through FindGameObjectsWithTag.

In parts of my script I need to change properties for all the windows, but how can I access a specific window (e.g, "Window01") ?

When I inspect the array, I can see they're not in any real order… Window04 Window07 Window02 etc.

…so I cannot simply do it numerically. Is there a way to perform a 'Find' within an array or is there another solution?

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
3
Best Answer

Answer by rutter · May 22, 2012 at 02:59 AM

The most important step here is to identify the criteria by which you'll be able to identify the particular window you're looking for. By name? By location? By some component property?

The simplest thing is to write a loop that runs through the list, looking for the item or items in question. You can write this as its own function, if you plan on performing these lookups very often.

Past that, you could use a language feature like Linq to write these lookups in shorthand. This can save time if you're a pretty experienced programmer, but is probably more trouble than its worth if you're just starting out.

Comment
Add comment · Show 4 · 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 Essential · May 22, 2012 at 03:05 AM 0
Share

Name is the only distinguisher. So something like…

 for (var i = 0; i < windows.Length; i++)
 {
    if ( window[i].name == "Window01")
    {
        Window01 = window[i];
        i = windows.Length;
    }
 }

…?

avatar image Berenger · May 22, 2012 at 04:16 AM 2
Share

If Window01 is declared before and if you use it after then yes, this is correct.

By the way, there is a keyword to break out of a loop, it's break;. You can use it ins$$anonymous$$d of i = windows.Length;

avatar image Essential · May 22, 2012 at 08:57 PM 0
Share

Oh cool, thanks Berenger :)

avatar image Essential · May 22, 2012 at 09:10 PM 0
Share

Hmm… One last question though… Is this really the easiest way to do this if I wish to be able to manipulate almost all of the items from that array?

So if I want to get Window01, Window02 all the way to Window07, I would need to have seven 'if else' statements within my for loop, which I'm sure won't be great for performance.

Unless there's some way that I can say:

 for (var i = 0; i < windows.Length; i++)
 {
     Declare Variable ( window[i].name ) = window[i];
 }

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

How to get the objects inside another object and put them in a array? 1 Answer

FindClosestEnemy() change target? 1 Answer

How do I instantiate same tag/name GameObjects with an array 1 Answer

Unity cannot find component of an object in an array. 2 Answers

how do I find components through variable 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