Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Going up/down a List?

Discussion in 'Scripting' started by kenaochreous, Mar 25, 2015.

  1. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    I have a script where I need a variable to increment up Resolutions/decrement down Resolutions. I tried t.text = resString[i + 1]; but then the problem occurs that i + 1 may be null. What other ways of going about this are there?
    Code (CSharp):
    1.     public List<string> resString = new List<string>();
    2.     // Use this for initialization
    3.     void Start () {
    4.             Resolution[] resolutions = Screen.resolutions; //all resolution
    5.         for(int i = 0; i <resolutions.Length; i++){
    6.             resString.Add(resolutions[i].width.ToString() + "x" + resolutions[i].height.ToString());
    7.             t.text = resString[i];
    8.         }
    9.     }
    10.    
    11.     public void resolutionFuncInc(Text t){
    12.     Resolution[] resolutions = Screen.resolutions; //all resolution
    13.         for(int i = 0; i <resolutions.Length; i++){
    14.             t.text = resString[i + 1]; // go up to the next element of resolutions
    15.         }
    16.     }
    17.    
    18.     public void resolutionFuncDec(Text t){
    19.     Resolution[] resolutions = Screen.resolutions; //all resolution
    20.         for(int i = 0; i <resolutions.Length; i++){
    21.             t.text = resString[i - 1];// go down to the next element of resolutions
    22.         }
    23.     }
     
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Something like this would allow you to go through the list without having problems with null:

    Code (CSharp):
    1.  
    2. Int curRes;
    3.  
    4. Public void dec(Text t){
    5. If(curRes > 0){
    6. curRes--;
    7. t.text = resString[curRes];
    8. }
    9. }
    10.  
    11. Public void inc(Text t){
    12. //Might be count not length for a list though, cant remember =p
    13. If(curRes < resStrings.Length-1){
    14. curRes++;
    15. t.text = resString[curRes];
    16. }
    17. }
    18.  
    19.  
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Last loop should be

    Code (CSharp):
    1. for (int i = resolutions.length; i > 0; i--)
     
  4. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Write a wrapper function...

    Code (csharp):
    1.  
    2. //  resString[wrapIndex(i + 1, resString.Length)]
    3. //  resString[wrapIndex(i - 1, resString.Length)]
    4.  
    5. static int wrapIndex(int i, int max)
    6. {
    7.     // ex:  i = -1, max = 5    ----    -1 % 5 = -1 + 5 = 4 % 5 = 4
    8.     // ex:  i =  1, max = 5    ----     1 % 5 = 1 + 5 = 6 % 5 = 1
    9.     return ((i % max) + max) % max;
    10. }
    11.  
    Jake
     
  5. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    I decided to go with lordconstant's method and I would like to include a cycler in the function. I was able to put one in inc but for dec it's either not occurring or it turns out null because of the if statements. Do I need to do something completely different for dec?
    Code (CSharp):
    1.  
    2.        //works fine
    3.       public void inc(Text t){
    4.         if(curRes < resString.Count-1){
    5.             curRes++;
    6.             t.text = resString[curRes];
    7.         }
    8.         else if(curRes >= resString.Count-1){
    9.             curRes = 0;
    10.             t.text = resString[curRes];
    11.         }
    12.     }
    13.  
    14.     //does not occur or turns out null
    15.     public void dec(Text t){
    16.         if(curRes > 0){
    17.         curRes--;
    18.         t.text = resString[curRes];
    19.         }
    20.         else if(curRes < 0){
    21.             curRes = resString.Count;
    22.             t.text = resString[curRes];
    23.         }
    24.     }
    25.    
     
  6. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    You have to set curRes to resString.Count - 1 instead of just resString.Count

    Code (csharp):
    1.  
    2. public void dec(Text t)
    3. {
    4.     if(curRes > 0)
    5.     {
    6.         curRes--;
    7.         t.text = resString[curRes];
    8.     }
    9.     else if(curRes < 0)
    10.     {
    11.         // Remember that arrays are 0 based
    12.         // and resString.Count gives you actual count.
    13.         curRes = resString.Count - 1;
    14.         t.text = resString[curRes];
    15.     }
    16. }
    17.