Search Unity

C# Keeping a String from going Outside of Array

Discussion in 'Scripting' started by kenaochreous, Feb 3, 2013.

  1. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    Hi everyone, I have a script that makes someString increment to next part of someArray.But if you keep clicking on the button then someString goes outside of someArray and causes a IndexOutOfRangeException.Any idea how to keep it in check?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SomeScript: MonoBehaviour {
    6. public string someString;
    7. public string[] someArray;
    8.     void Start(){
    9. someArray = new string[]{"text","text2","text3"};
    10. someString = someArray[0];
    11.     }
    12.     void OnGUI(){
    13. if (GUILayout.Button("MoveUpOne", GUILayout.Width(50), GUILayout.Height(25))){
    14. someString = someArray[System.Array.IndexOf(someArray, someString) + 1];
    15.     }
    16.   }
    17. }
    18.  
    19.  
     
  2. fano_linux

    fano_linux

    Joined:
    Jan 1, 2012
    Posts:
    909
    You need to check if the next array position you are trying to get is != null before getting it's value.
     
  3. Elryk

    Elryk

    Joined:
    Jan 2, 2013
    Posts:
    82
    If the array changes size, you may want to look into using generic lists instead. With generic lists, you dont have to worry about the size of the array as much.

    If you are set on using arrays, you could have a check on your index to make sure it doesn't go beyond the limits of the array.

    For line 13:
    It looks like you would need to assign System.Array.IndexOf(someArray, someString) + 1 to a variable, check that variable to make sure its in bounds, then let line13 do its thing replacing the system.array........ part with the variable you created and checked previously. If the variable is out of bounds of the arrays size, simply do nothing.

    I'd give you a code example, but I am not fluent with C#, so I hope the information I have provided is helpful enough to set you strait with this.
     
    Last edited: Feb 3, 2013
  4. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    Try

    someString = someArray[Mathf.Min(System.Array.IndexOf(someArray, someString) + 1, someArray.Length -1)];
     
  5. laakerules

    laakerules

    Joined:
    Aug 29, 2012
    Posts:
    153
    try this it works better and if you want to get an array from the list just do .toArray();

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5. using System.Collections.Generic;
    6.  
    7.  
    8.  
    9. public class SomeScript: MonoBehaviour {
    10.  
    11. public string someString;
    12.  
    13. public List<string> someArray = new List<string>();
    14.  
    15.     void Start(){
    16.  
    17. someArray.add("text");
    18. someArray.add("text1");
    19. someArray.add("text2");
    20.  
    21. someString = text;
    22.  
    23.     }
    24.  
    25.     void OnGUI(){
    26.  
    27. if (GUILayout.Button("MoveUpOne", GUILayout.Width(50), GUILayout.Height(25))){
    28.  
    29.  
    30. someArray.add(someString);
    31.  
    32.     }
    33.  
    34.   }
    35.  
    36. }
    37.  
    this is psuedo code btw so dont yell at me if theres errors, just fix them by reading the errors, the errors output some pretty common sense sometimes.
     
  6. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    That works pretty well, but what if I wanted to go backwards? I fiddled around with it but couldn't get it to decrease without going over the array's limit.



    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6. public string someString;
    7. public string[] someArray;
    8.     void Start(){
    9. someArray = new string[]{"text","text2","text3"};
    10. someString = someArray[2];
    11.     }
    12.     void OnGUI(){
    13. if (GUILayout.Button("MoveDownOne", GUILayout.Width(50), GUILayout.Height(25))){
    14. //my guess
    15. //someString = someArray[Mathf.Min(System.Array.IndexOf(someArray, someString) -1, someArray.Length +1)]
    16.     }
    17.   }
    18. }
    19.  
     
  7. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    To go down, try:
    Code (csharp):
    1.  
    2. if (GUILayout.Button("MoveDownOne", GUILayout.Width(50), GUILayout.Height(25)))
    3. {
    4.     someString = someArray[Mathf.Max(System.Array.IndexOf(someArray, someString) -1, 0)]
    5. }
    6.  
     
  8. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    It works great,Thanks for the help.:)
     
  9. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    No worries :)