Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Thephil2988 · Jan 26, 2018 at 04:32 PM · listintfindindexsearch

Getting the Index number of lowest 7 ints in list

I have a case where a certain amount of shots are used in each round and put into an int. I have several rounds and store them in a list. I want to know the index values of the 7 lowest ints in the list so i can find the rounds where the least amount of shots were done. I have looked at doing something like shots.FindIndex but i cant make it work. Any help would be appreciated

Code:

 public List<int> shots = new List<int>();
     public int bestScore;
     
 
     // Use this for initialization
     void Start()
     {
         hits.Add(0);
         hits.Add(1);
         hits.Add(2);
         hits.Add(3);
         hits.Add(4);
         hits.Add(5);
         hits.Add(6);
         hits.Add(7);
         hits.Add(8);
         
     }

Comment
Add comment · Show 1
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 KittenSnipes · Jan 26, 2018 at 11:50 PM 0
Share

@thephil2988

So what you are trying to do is go through the list and find the one with the lowest amount of shots?

3 Replies

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

Answer by KittenSnipes · Jan 27, 2018 at 12:14 AM

@Thephil2988

I think this is what you are going for. I typed it on my phone so I am not absolutely sure it will do what I think so it is a bit untested:

      public List<int> shots;
      public int bestScore;
      
  
      // Use this for initialization
      void Start()
      {
          shots = new List<int>();
          hits.Add(0);
          hits.Add(1);
          hits.Add(2);
          hits.Add(3);
          hits.Add(4);
          hits.Add(5);
          hits.Add(6);
          hits.Add(7);
          hits.Add(8);
      }

    int Min() {
          int lowestShots = INT_MAX;
          for (int i = 0; i < hits.Count; i++ ) { 
              if (lowestShots < hits[i]) {
                  lowestsShots = hits[i];
              }
              return lowestsShots;
          }
      }

      void Update() {
          int lowestAmountOfShots;
          lowestAmountOfShots = Min();
      }
 
Comment
Add comment · Show 12 · 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 Thephil2988 · Jan 28, 2018 at 08:37 PM 0
Share

Sorry for the late answer, but i cant seem to make it work... I set the lowestShots variable to a number that is high enough to make sure no index on the list is bigger. The variable i get from the function is just the number i declare lowestShot as before the for loop

Here is the code:

    public List<int> hits = new List<int>();
     public int bestScore = 0;
     
 
 
     // Use this for initialization
     void Start()
     {
         hits.Add(0);
         hits.Add(1);
         hits.Add(2);
         hits.Add(3);
         hits.Add(4);
         hits.Add(5);
         hits.Add(6);
         hits.Add(7);
         hits.Add(8);
 
         bestScore = $$anonymous$$in();
         print(hits[8]);
 
         
     }
 
     int $$anonymous$$in()
     {
         int lowestShots = 10000000;
         for (int i = 0; i < hits.Count; i++)
         {
             if (lowestShots < hits[i])
             {
                 lowestShots = hits[i];
             }
         }
         return lowestShots;
     }
 
avatar image KittenSnipes · Jan 28, 2018 at 08:49 PM 0
Share

Inside print ins$$anonymous$$d of putting hits[8] put bestScore

avatar image Thephil2988 KittenSnipes · Jan 28, 2018 at 09:05 PM 0
Share

Oh... i didnt revert that... that was just me trying some stuff. I still get the lowestShot value

avatar image KittenSnipes Thephil2988 · Jan 28, 2018 at 09:07 PM 0
Share

Set lowestShots = 0 not that high number. Otherwise it will never work

Show more comments
avatar image
1

Answer by Alanisaac · Jan 28, 2018 at 09:16 PM

Another way to do this would be to use LINQ. You can use anonymous objects to make this a little easier:

          hits.Select((x, i) => new { Shots = x, Index = i })
              .OrderBy(x => x.Shots)
              .Take(7)
              .Select(x => x.Index);
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 Thephil2988 · Jan 28, 2018 at 09:22 PM 0
Share

How do i make LINQ work in unity? i have seen other people talk about it when it comes to lists before

avatar image Thephil2988 · Jan 28, 2018 at 09:25 PM 0
Share

Does it return a list and if so, how do the syntax work?

avatar image Alanisaac · Jan 28, 2018 at 10:30 PM 0
Share

This returns an enumerable, which is essentially something you can use in a foreach loop. I put this into the context of your script above. Your original post mentioned that you wanted not just the lowest index, but the seven lowest indexes. You can use this script to get any number of lowest indexes you want:

      public List<int> hits = new List<int>();
      public List<int> shots = new List<int>();
      public int bestScore = 0;
     
      // Use this for initialization
      void Start()
      {
          hits.Add(15);
          hits.Add(2);
          hits.Add(56);
          hits.Add(24);
          hits.Add(6);
          hits.Add(74);
          hits.Add(42);
          hits.Add(52);
          hits.Add(42);
  
          var threeLowestIndexes = LowestIndexes(3);
          foreach(var index in threeLowestIndexes) 
          {
              print(index);
 
              // you should see:
              // 1
              // 4
              // 0
              // which are the indexes in the above list corresponding to 2, 6, 15.
          }
  
          
      }
  
      IEnumerable<int> LowestIndexes(int count)
      {
         return hits.Select((x, i) => new { Shots = x, Index = i })
                    .OrderBy(x => x.Shots)
                    .Take(count)
                    .Select(x => x.Index);
      }
avatar image
0

Answer by victorbisaev · Jan 28, 2018 at 09:52 PM

Create a list of records of type ("number of hits", "index in the array"), fill it with actual values based on "hits" list, then sort the list (by "number of hits") and use items with indices 0..6, extracting corresponding "index in the array" from the sorted list and use it as an index into "hits" like "hits[sortedList[0].indexInHits]". LINQ above perform the same procedure but there are some concerns about using LINQ in Unity (check https://forum.unity.com/threads/to-linq-or-not-to-linq.223887/)

Comment
Add comment · 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

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

78 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 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

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

How to search a LIST(not an array) for game objects with a specific tag? 1 Answer

How I randomise a list of signals in Unity and remove it from the list once the clip is finished? 0 Answers


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