• 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
Question by ironblock · Jun 10, 2016 at 08:45 PM · listindexforloop

index number problems Forloop

i cant seem to get the right index number. it does set the lowest number but it always returns i = 0 in the debug log.

it must be somthing simple but i cant see it.

 public void  FindClosest(){
         if (CurrentTargets.Count == NodeDistances.Count) {
             int Lowest = 99999;
             int indexNumber = 0;
             for (int i = 0; i < NodeDistances.Count - 1; i++) {
                 if (Lowest > NodeDistances [i]) {
                     Lowest = NodeDistances [i];
                     indexNumber = i;
                     Debug.Log (i + "lowest Number" + Lowest );
                 }
             }
             target = CurrentTargets [indexNumber];
 
             PathRequestManager.RequestPath (transform.position, target.position, OnPathFound);
         }
     }
 




Comment

People who like this

0 Show 4
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 Mmmpies · Jun 11, 2016 at 11:26 AM 0
Share

Are you sure NodeDistance[0] isn't simply the lowest value in the array?

avatar image ironblock Mmmpies · Jun 11, 2016 at 11:35 AM 0
Share

i have 3 other targets in the array,even if 0 = 27 units and 1 = 4 units and 2 = 6 units it still goes to [0].

avatar image Mmmpies ironblock · Jun 11, 2016 at 12:24 PM 1
Share

I can't make this code fail!

I've tried with both an int array and changing Count to Length and then, because that worked perfectly, I switched to a List List and back to Count, it just seems to work every time. I can post the entire script if you want but it's almost identical to yours.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by jgodfrey · Jun 11, 2016 at 12:16 PM

Your loop indexer is wrong and will cause the last array element to not be processed. This:

 i < NodeDistances.Count - 1

Should be:

 i < NodeDistances.Count 

That said, I don't see how that could cause the problem you describe (based on the mentioned sample data). Nothing else jumps out at me, but it should be easy to find by adding some additional Debug.Log statements. Dump everything you're working with in the loop (current index, current value, Lowest" value, etc) and see what's not right.

Comment
ironblock

People who like this

1 Show 1 · 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 ironblock · Jun 12, 2016 at 11:32 AM 0
Share

it was in deed the -1. i found it after taking a break. but thanks annyway :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

61 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

Related Questions

I need help finding the index of an object in a list. 1 Answer

Trying to index a list, gives error. 1 Answer

Unknown Argument Out of Range Index Error On Card Game 1 Answer

[ANSWERED] IndexOf and LastIndexOf both returning -1 when an item is on the list. 1 Answer

Insert object to the list in the script in real-time. 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