Search Unity

Question String array index ou of range exception

Discussion in 'Scripting' started by Kernkraft, Nov 5, 2020.

  1. Kernkraft

    Kernkraft

    Joined:
    Oct 26, 2020
    Posts:
    32
    Hello, i always get an error when i come to line 7 in the scond block:
    Code (CSharp):
    1. public float[] positionGameobject1;
    2.     public string[] gameObjectName;
    3.     private int gameObjectNameNumber;
    4.     private int positionGameObject1Number;
    Code (CSharp):
    1.  public void PlaceSchmiede()
    2.     {
    3.         Debug.Log("Button pressed");
    4.         placeableObjectPrefab = Resources.Load("Prefabs/Schmiede") as GameObject;
    5.         Debug.Log("Button pressed1");
    6.         Debug.Log(gameObjectNameNumber);
    7.         gameObjectName[gameObjectNameNumber] = "Schmiede";
    8.         Debug.Log("Button pressed2");
    9.         gameObjectNameNumber++;
    10.         Debug.Log("Button pressed3");
    11.         benutzernameSpeichern.SaveBuildingsName();
    12.         Debug.Log("Button pressed4");
    13.         buttonSchmiedeClicked = true;
    14.         Debug.Log("Button pressed5");
    15.     }
    upload_2020-11-5_16-13-55.png

    I think i am doing wrong with this string array "gameObjectName" but i dont know what is wrong.
    It should check for the first entry in the array so 0 and put there the string "Schmiede" but instead i get an errormessage.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
  3. Kernkraft

    Kernkraft

    Joined:
    Oct 26, 2020
    Posts:
    32
    Hey thank you for your help.
    When write this line:
    Code (CSharp):
    1. public string[] gameObjectName = new string[10];
    I will get a string array with 10 emptys to fill.
    They go from 0 to 9 when i want to go direktly to the slots.
    (hope this is correct)

    So when the value "gameObjectNameNumber" is 0 the string "Schmiede" has to be written in the array "gameObjectName" to position 0.
    Am i thinking the right way here?
    Code (CSharp):
    1. public void PlaceSchmiede()
    2.     {
    3.         Debug.Log("Button pressed");
    4.         placeableObjectPrefab = Resources.Load("Prefabs/Schmiede") as GameObject;
    5.         Debug.Log("Button pressed1");
    6.         Debug.Log(gameObjectNameNumber);
    7.         gameObjectName[gameObjectNameNumber] = "Schmiede";
    8.         Debug.Log("Button pressed2");
    9.         gameObjectNameNumber++;
    10.         Debug.Log("Button pressed3");
    11.         benutzernameSpeichern.SaveBuildingsName();
    12.         Debug.Log("Button pressed4");
    13.         buttonSchmiedeClicked = true;
    14.         Debug.Log("Button pressed5");
    15.     }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    That is correct. Arrays and Lists are zero-based, so 10 entries is from 0 to 9.
     
    Kernkraft likes this.
  5. Kernkraft

    Kernkraft

    Joined:
    Oct 26, 2020
    Posts:
    32
    Hmm but i am getting the same error from this.
    Creating an array like this:
    Code (CSharp):
    1.     public string[] gameObjectName = new string[10];
    2.  
    Then put some Data in:
    Code (CSharp):
    1. gameObjectName[0] = "Schmiede";
    Same error as bevor:
    upload_2020-11-5_18-9-7.png
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,004
    It's a public string array. Therefore it will be serialized in the inspector. So it doesn't matter what you define in the field initializer. So if you don't want this variable to be serialized in the inspector, either make it private or mark it with the NonSerialized attribute. If you want the array to be serialized and you also want to "recreate" the array in code, move the creation of the array to Awake or inside your method where you want to set the string(s).
     
    Kernkraft likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Good catch @Bunny83, that's probably what is wrong.

    @Kernkraft here is another way to go for initializing arrays:

    Code (csharp):
    1. List<string>TempList = new List<string>();  // empty
    2.  
    3. TempList.Add( "Hello!");
    4. TempList.Add( "Goodbye!");
    5.  
    6. // and now we can make the array
    7.  
    8. gameObjectName = TempList.ToArray();
    and now
    gameObjectName
    has "Hello!" and "Goodbye!" and is size 2.
     
    Kernkraft and Bunny83 like this.
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,004
    In this case it would make more sense to just do

    Code (CSharp):
    1. gameObjectName = new string[] {
    2.     "Hello!",
    3.     "Goodbye!"
    4. };
    Though using a List may be useful when you "outsource" the filling of the array / list to other methods so you can simply pass the list along and have them fill in the content.
     
    Kernkraft likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I like that ... "outsourcing." Fitting.

    And I think that's kinda what OP is doing... he seems to be filling it in some kind of button responder function.
     
    Kernkraft likes this.
  10. Kernkraft

    Kernkraft

    Joined:
    Oct 26, 2020
    Posts:
    32
    Many thanks to both of you your info helped me a lot :D
    I did the List thing get rid of the error and then transform it back to an array and safe it to a file.

    But also i have another question about the list.
    Is it possible to write in a specific position?
    Like this:
    Code (CSharp):
    1. public List<string> gameObjectName = new List<string>();
    2.  
    3. gameObjectName[1].Add("Hello")
    4. gameObjectName[3].Add("Goodbye")
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Of course, as long as it is within the bounds of the array (0 or greater and less than .Length)
     
    Kernkraft likes this.
  12. Kernkraft

    Kernkraft

    Joined:
    Oct 26, 2020
    Posts:
    32
    Finally got my object saving working.
    Thanks a lot again.
     
    Kurt-Dekker likes this.