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 Buzzdev · Nov 26, 2014 at 10:21 PM · looplists

Looping list information into another list

I'm trying to create a menu that shows a certain amount of levels per page, (so more than 6 levels will add another page).

But my pages.levels list is showing the "Level 01" 6 times in both page lists, where it should be the first 6 levels in the first and only level 07 in the second (I'm using 7 strings as 'levels' currently)

alt text

PagesHandler.cs public class PageHandler { public int index; public List pages; }

Page.cs' public class Page { public List levels; }

This is my MainMenu.CS

 public List<string> levels;
 
     public PageHandler pageHandler;
 
     int pageCount = 1;
 
     int count = 0;
     
     void Start () {
 
         //Check How Many Pages Needed
         for (int i = 0; i < levels.Count; i++) {
                 
             if (count == 5) {
 
                 pageCount++;
             
                 count = 0;
 
             } else { 
             
                 count++;
         
             }
                 
         }
 
         //Set Page Handler
         pageHandler = new PageHandler ();
     
         pageHandler.pages = new List<Page>(pageCount);
 
         for (int j = 0; j < pageCount; j++) 
         {
 
             pageHandler.pages.Add(new Page());
 
             pageHandler.pages[j].levels = new List<string>();
 
         }
 
         //Add levels to page
         for (int k = 0; k < levels.Count; k++)
         {
                     
             for (int l = 0; l < pageHandler.pages.Count;)
             {
 
                 if(pageHandler.pages[l].levels.Count == 6)
                 {
 
                     l++;
                 
                 } else { 
                 
                     pageHandler.pages[l].levels.Add(levels[k]);
                     
                 }
                 
             }
             
         }
 
     }


Thanks!

screen shot 2014-11-27 at 19.34.47.png (34.9 kB)
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
0
Best Answer

Answer by richyrich · Nov 27, 2014 at 08:50 PM

Hi. Unfortunately, your page counting fails on several points. In these situations, it is often a good idea to revert to pen and paper to consider why it does not work. Let's look at your 'Check How Many Pages Needed' loop. In the first column we write the index, in the second we write what the value of count is when we check it and finally we write the value at the end of that particular cycle (iteration) of the loop...

 Index  Check  Count
 0      0      1
 1      1      2
 2      2      3
 3      3      4
 4      4      5
 5      5      0 //This is due to the else (we should actually increment regardless)
 6      0      1

So by the end of the loop, we have an error in that we've lost an entry

Later on in the program, we ignore the fact that count now is equal to one. We should have added an if-statement at the end saying that is count>0, pageCount++; However, you're checking for == 5, but elsewhere you have stated that you want 6 entries to a page, so you have another error of logic there. Sorry.

Here's the light at the end of the tunnel though... you don't even need the loop ;)

Instead, you need to use the modulus operator (%). This works like division, only it tells you the remainder of a division.

So for example 9/4 = 1 (9/4 [using whole values == 2], but the remainder is 1

So in your context, we can change your for loop to:

 int entriesPerPage = 6;
 int rem = levels.Count% entriesPerPage;
 pageCount = (levels.Count- rem) / entriesPerPage;
 if (rem > 0)
     pageCount++;

Part II!

Try to avoid nested loops if you can, they can make debugging more difficult. This next piece of code I've not spent too much time on, so have a play first. Any problems, just add a comment and let us know :D

     int nextlevels = entriesPerPage;
     int currentPage = 0;
     for(int i=0;i<levels.Count;i=i+entriesPerPage)
     {
         if (nextlevels > (levels.Count - 1)) //might not need -1, brain index failure ;)
         {
             nextlevels = levels.Count - 1;
             pageHandler.pages[currentPage].levels = levels.GetRange(i, nextlevels);
             break;
         }
         else
         {
             pageHandler.pages[currentPage].levels = levels.GetRange(i, nextlevels);
         }
         nextlevels += 6;
     }
Comment
Add comment · Show 6 · 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 Buzzdev · Nov 28, 2014 at 09:11 AM 0
Share

Cheers for the great reply! (I'd upvote you too but unfortunately I don't have the 'reputation' yet)

I've never thought to write down iterations like that, looks very useful, i'll be doing that a lot from now on!

The 5 at the start and the 6 later on was just a preference change, that I embarrassingly forgot to fix!

That modulus operator section looks brilliant as well, a lot less messy too! Just overall a great reply !

Thank you I've learned a lot!

avatar image richyrich · Nov 28, 2014 at 11:07 AM 0
Share

Glad I was able to help.

The 5 at the start and the 6 later on was just a preference change

Problems like these can be avoided using named values; the code above uses int entriesPerPage = 6; Note: the last line of code (nextlevels += 6;) should have been nextlevels += entriesPerPage;

Named values improve readability and reduce bug opportunities. This can further be improved by not allowing it to be changed. You could declare it as a constant at the top of the class as: public const int EntriesPerPage = 6; Now the only place to change would be at declaration.

avatar image Buzzdev · Nov 28, 2014 at 05:16 PM 0
Share

Thanks again for the response and the good advice!

Sorry for being 'that guy', but I'm getting an error with this, I thought I understood it seems I don't.

I'm getting this error: ArgumentException: index and count exceed length of list

$$anonymous$$y gut feeling is that it's the 'i' on the second iteration being 12? Hard to know cause i'm always having brain index failure!

Using int nextlevels = entriesPerPage; int currPage = 0;

         for(int i = 0; i < levels.Count; i += entriesPerPage)
         {
 
             if (nextlevels > levels.Count) 
             {
 
                 nextlevels = levels.Count - 1;
                 pageHandler.pages[currPage].levels = levels.GetRange(i, nextlevels);
                 break;
 
             } else {
 
                 pageHandler.pages[currPage].levels = levels.GetRange(i, nextlevels);
 
             }
     
             currPage++;
             nextlevels += 6;
                 
         }
avatar image richyrich · Nov 28, 2014 at 06:47 PM 0
Share

You are just exceeding the number of elements inside the levels List. I won't be able to do anything more until tomorrow. But message received, will look at it again unless you fix it in meantime.

You're not 'that guy'! Chill, it'll get fixed :D

avatar image richyrich · Nov 30, 2014 at 01:28 AM 0
Share

Try this:

 int nextlevels = 6;
 for(int i = 0;i < levels.Count;i=i + 6)
 {
     if (i+nextlevels >= levels.Count)
     {
         nextlevels = levels.Count - i;
         pageHandler.pages[currPage].levels = levels.GetRange(i, nextlevels);
         break;
     }
     else
     {
         pageHandler.pages[currPage].levels = levels.GetRange(i, nextlevels);
     }
     currPage++;
 }
Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I fix audio loop delay 2 Answers

Scripting: make thing repeating 3 Answers

my foreach loop and condition wont work 0 Answers

Move a Person to a point / Repeat a function called from another script? 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