- Home /
Multidimensional array trouble
Hi guys, I have this problem. I'm trying to make a multidimensional array like this:
var myArray = new Array();
myArray[0] = new Array("Pera", "Manzana", "Naranja");
myArray[1] = new Array("Rojo", "Azul", "Amarillo");
myArray[2] = new Array("Verde", "Morado", "Marrón");
Debug.Log(myArray[1][2]); //should by "Amarillo" but get this " Type 'Object' does not support slicing."
I get the mistake that Object does't support slicing!! And I don't know how to show the correct value. Thank you so much.
Answer by fafase · Aug 24, 2015 at 04:04 PM
First solution, do not use Array, they are prone to error a they are not strongly type and then they make it all slow as you need casting everywhere.
The solution is to use jagged arrays or "jagged list" maybe in your case. You could also think of 2D array. The choice will depend on how much you know at start.
Here is an example:
var myList = new List<string[]>();
myList.Add(new string[]{"Pedro", "Diego"});
myList.Add(new string[]{"Jesus", "Juan", "Fernando"});
Here you are kinda free but you would have to check for the length of the array to avoid getting out of it.
It is strongly typed as only string array as accepted and you can add arrays of any sizes.
Answer by Cherno · Aug 24, 2015 at 06:15 PM
Use a multi-dimensional array like this:
string[,,] myArray = new string[3,3] {
{ "Pera", "Manzana", "Naranja" },
{ "Rojo", "Azul", "Amarillo" } ,
{ "Verde", "Morado", "Marrón" }
};
Debug.Log(myArray[1,2]);//= Amarillo
Your answer
Follow this Question
Related Questions
Multidimensional arrays - editing one element within one of the arrays 1 Answer
How to remove null's from 2d array ? It's removing but only 2 out of 4 null's. 0 Answers
Changing images using a shader on a 2D canvas? 0 Answers
Problems adding GameObjects to a GameObject array (C#) 2 Answers
Making an array for Texture2D?? 2 Answers