ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
That error really like an annoying kid. First I tried
public string[] debugstrings;
and when I do this;
debugtext.text = debugstrings[1];
I want to delete first element of array. But why I don't know C# doesn't supports the normal array functions like RemoveAt(); And I tried with ;
public System.Collections.Generic.List<string> debugstrings;
That works perfectly. But when I want
debugtext.text = debugstrings[1];
There are an error.. :'<
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[System.String].get_Item (Int32 index).....
Why this happening and what is this. Why unity doesn't make any real solution for arrays? Thats a small thing and I must focus other important things. But why I must get an error every time? Why this happens..? Please help..
With the list method it should work fine. At least it does here :
using UnityEngine;
using UnityEngine.UI;
public class test3 : $$anonymous$$onoBehaviour {
public System.Collections.Generic.List<string> debugstrings;
public Text debugtext;
void Start(){
debugtext.text = debugstrings[1];
debugstrings.RemoveAt (1);
}
}
And it's possible to do the same with
public string[] debugstrings;
but it requires a bit more coding.
I know but when I do that ı get the same error.
That error really like an annoying kid. First I tried
public string[] debugstrings;
and when I do this;
I wrote here its easiest way but there is no support for real functions like RemoveAt();
Answer by pgomes · Aug 28, 2015 at 01:33 PM
Make sure you always initialize your builtin arrays before using them:
debugstrings = new string[<IMMUTABLE-SIZE>];
In C# you can't resize builtin arrays. From the documentation:
There are two types of arrays in Unity, builtin arrays and normal Javascript Arrays. Builtin arrays (native .NET arrays), are extremely fast and efficient but they can not be resized
Use a mutable size container instead, such as a List.
That's not interly true using System;
Array.Resize(ref myArray, newSize);
Should do the trick.I can't write an example at the moment. I'll do it later tho. :P
System.Array is a mutable size container and it is not what the documentation defines as builtin array. I would continue to argue that if you are going to be removing elements from debugstrings you might be better off with a List. For pros and cons check http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which#comment27660729_434761