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 /
This question was closed Jul 08, 2017 at 12:52 PM by abdo400 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by abdo400 · Jul 07, 2017 at 10:58 PM · scripting problemscriptingbasics

How to get the next element in a list?

Hey folks, I have this function:

      int DecideNumberOfSlots ()
 {
     int noOfSlots = 0;
     List<int> storedID = new List<int> ();

     for (int i = 1; i < cmManager.playerArmyList.Count; i++) {
             if (!storedID.Contains (cmManager.playerArmyList [i].ID)) {
                 if (cmManager.playerArmyList [i].ID != cmManager.playerArmyList [ i + 1 ].ID) {
                     storedID.Add (cmManager.playerArmyList [i].ID);
                     noOfSlots++;
                 }
             }
         }

     return noOfSlots;
 }  

which is supposed to count how the different troop types in the list by their IDs, however this code was working perfectly then suddenly stopped and gave me (argument out of range error), when I made (i = 1) and (cmManager.playerArmyList [ i - 1 ].ID) it works but always returns zero if their is only one type of troops.

Any ideas how could I acces the next element in the list or just improve the code?
Thanks

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

4 Replies

  • Sort: 
avatar image
1

Answer by aldonaletto · Jul 08, 2017 at 01:31 AM

If you just want to know how many different IDs are in ArmyList, this code would be simpler:

 int DecideNumberOfSlots () {
     List<int> storedID = new List<int> (); // create a temporary list of IDs
     for (int i = 0; i < cmManager.playerArmyList.Count; i++) {
         int id = cmManager.playerArmyList[i].ID; // get the ID only once
         if (!storedID.Contains(id)){ // if not yet in the list...
             storeID.Add(id); // add it
         }
     }
     return storedID.Count; // the list size is the number of different IDs
 }

I didn't get it why you're comparing each element with the next one before adding a new ID to the list: besides causing the "out of range" error, this could make you miss the last ID.

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
avatar image
0

Answer by Jwizard93 · Jul 07, 2017 at 11:42 PM

This is messy. But a quick fix:

None of this makes sense unless there's more than one thing in the list right?

Test that there is at least 2 things in the list before you do any of this. It will be even messier.

try this style

 if
 {
     if
     {
      
     }
 }

its at least easier to see whatsup.

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
avatar image
0

Answer by abdo400 · Jul 08, 2017 at 12:03 AM

I think I got it: Should make a new list and loop through thr army list then add every id that isn't in the new list, this isn't tested yet but I think it will work.
If anybody disagree with the logic please comment why

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
avatar image
0

Answer by ShadyProductions · Jul 08, 2017 at 12:12 AM

     private int DecideNumberOfSlots()
     {
         return cmManager.playerArmyList.Select(f => f.ID).Distinct().Count();
     }

You could maybe try something like this, returns the count of the unique Id's Make sure you reference using System.Linq;

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

Follow this Question

Answers Answers and Comments

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

Having trouble using SendMessage 1 Answer

How would i make it transform player.position =target.position? 2 Answers

Moving a ScrollRect child via script to focus on some point. 0 Answers

how to allow the key to only open 1 door rather than all of them? 0 Answers

Scan for a variable based upon an integer 2 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