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 Romano185 · Jun 23, 2012 at 12:21 PM · arrayloopfor looplength

Loop through array until certain value is found.

Hi everybody

I've made 2 scripts, 1 holds an array of gameobjects and I want the second script to search for the gameobject it's attached to in the array. If this object is found I want this script to tell me in which position in the array this object can be found. How can I do this? I have been told that I must use a for loop to loop through the array, but the array also changes size. I've told the array to add 1 to its length if it's completely filled.

Please help me solve this problem.

Comment
Add comment · Show 2
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 Fattie · Jun 23, 2012 at 08:11 PM 0
Share

Don't use arrays. Use "List".

avatar image Mizuho · Jun 24, 2012 at 08:41 AM 0
Share

@Fattie: List uses an array internally anyway, so if you want to give yourself the extra headache, I say go for it xD.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Jun 23, 2012 at 01:15 PM

Don't use an array, use a List.\ by importing System.Collections.Generic

List has an IndexOf function that will give you what you want. You can still write code that looks like an array - the only difference is use Count rather than length for the number or items.

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 Romano185 · Jun 23, 2012 at 03:41 PM 0
Share

Thanks, that sounds pretty useful. I'll try it as soon as possible and I'll tell if it worked as I expect it to work.

avatar image Romano185 · Jun 23, 2012 at 06:46 PM 0
Share

But how can I make sure that all items in my List are different? Is there a special way to do that?

avatar image whydoidoit · Jun 23, 2012 at 08:24 PM 0
Share

Ah yes, you can use a HashSet.\ or a Dictionary keyed off GameObject.

avatar image
1

Answer by Drakestar · Jun 23, 2012 at 01:18 PM

The array should not change size while you're iterating over it (if it is, stop - you need to rethink your approach to what you're doing). You can just do a simple loop using a counter i, and access whatever element satisfies your search criteria by breaking and returning the value of i when you're found it.

 int index = -1;
 for (int i = 0; i < myArray.Count; i++)
 {
     if (myArray[i] == <whatever criteria you specify>)
     {
         index = i;
         break;
     }
 }
 
 if (index != -1)
     <act on successful search>
 

Alternatively, you might consider using a .Net list instead, and search for your object using List.FindIndex() using a Lambda. It returns the index of the element. For example

 int index = myList.FindIndex(o => o.Name == "ObjectNameImLookingFor);
 if (index != -1)
     <act on successful search>
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 Jessy · Jun 23, 2012 at 02:50 PM 1
Share
  • sucks. $$anonymous$$ill it with fire!

http://stackoverflow.com/questions/10864947/is-there-a-practical-reason-for-why-listt-indexoft-does-not-return-a-nullabl

avatar image Drakestar · Jun 24, 2012 at 08:26 AM 0
Share
  • is a reality in .NET ILists. It is clearly documented, not bound to be confusing (because in C# arrays and lists -1 is never a valid index, so -1 takes on its own, documented meaning) and used all over the world in professional code. I'm not going to discourage people from using it because of some ideological righteousness. The #1 StackOverflow answer rightfully points out that "Code complete also maintains that you must meet the contracts you agree to and therefore List.Indexof must return -1" - so "sucks, kill it with fire" strikes me as a disproportionate reaction unless you can cite specific examples of where -1 leads to actual (not ideological!) problems in production code.

avatar image Mizuho · Jun 24, 2012 at 08:40 AM 0
Share

@Drakestar: Only thing that can possibly counter your argument (as far as I know) is the custom scripting system from Ragnarok Online. -1 wrapped around the array on there so it would return the last value in the array.

avatar image Drakestar · Jun 24, 2012 at 08:54 AM 0
Share

@$$anonymous$$izuho Yes - I'm sure there's reasons (I don't know Ragnorak Online, but it sounds like even there this wouldn't cause confusion because the -1 was the result of a search operation, where it retains its "element not found" meaning). But unless those reasons actually affect the person who asked this question, and while we're on a board with a large population of people getting their bearing in program$$anonymous$$g, I don't see much use in getting into largely ideological arguments. Especially when the argument is backed up by a StackOverflow article that itself was "closed as not constructive" by multiple 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

7 People are following this question.

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

Related Questions

how do i make a loop for a boolean? 2 Answers

Method to loop through an unknown number of variable combinations in C# 1 Answer

variables based on array.length not updating in JS 1 Answer

Find the length of a string in Javascript 1 Answer

Trouble with for loop out of range. Simple? Maybe. 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