- Home /
Array index is out of range
i get the error above in this line of code:
RandomNumber = Random.Range(0, 4);
Word = Database[RandomNumber];
----->WordinUse = Word.Split(""[0]);
why is this? btw the database array is : var Database = ["eu","joao","lampada"] ;
im sorry if this is a stupid mistake, im new to unity and javascript.
Thanks in advance for the help!
Answer by robertbu · Mar 26, 2013 at 03:37 AM
With the ""[0], you are specifying an empty string (length is zero), and then you are trying to index into the first character. Assuming you are trying to split on a space, leave a space between the two quotation marks.
No, i was trying to split at every character! How can i do that?
You can use the function in this post:
http://answers.unity3d.com/questions/169442/split-a-string-every-n-characters.html
That seems like overkill. If you are just trying to index into the string you can do:
var theChar = Word[0];
to access a specific character (in this case the first one).
I don't think it buys you anything over indexing into the string, but you can do something like:
var array_of_characters = Word.ToCharArray();
http://msdn.microsoft.com/en-us/library/ezftk57x.aspx
This will "split" your string into an array of characters.