- Home /
Can anyone help me with a system that helps me to access data inside of data?
I followed lessons on codeacademy in Javascript but they don't seem to apply in Unity. What I would like to achieve is accessing data inside of data like to following example:
var array01 = [1,2,3,4]; var array02 = [5,6,7,8,9,10]; var arrayBoth = [array01, array02];
// I would like to acces data with one command like:
arrayBoth[1][2];
Can anyone help me with a system that helps me to access data inside of data?
Unity does not use Javascript like the one you learn. try the learn section from Unity
Answer by CHPedersen · Nov 13, 2014 at 02:20 PM
codeacademy's JavaScript tutorial is for Web JavaScript, which is not at all the same as Unity's poorly named JavaScript, also sometimes referred to as "UnityScript" to clear the confusion. Their tutorial does not apply whatsoever to Unity, save perhaps superficial syntax familiarity.
You declare, instantiate and access arrays in UnityScript like so:
// Array declaration:
var arrayExample : int[];
// Array instantiation:
arrayExample = new int[10];
// Array access:
var fifthElement = arrayExample[5];
Example of rectangular array syntax in JS:
// Declaration of rectangular array:
var rectArray : int[,];
// Instantiate the rectangular array
rectArray = new int[3,3];
// Set the value of an element inside the array (at column = 2, row = 2) to 10
rectArray[2,2] = 10;
// Access the element at that position again and store it in a local variable:
var twoPointTwo = rectArray[2,2];
Debug.Log("Value of element at 2,2: " + twoPointTwo);
I have updated my reply with sample syntax that applies to rectangular arrays.
Thanks! It works! What would be the changes if I want store strings ins$$anonymous$$d of numbers? Changing just int to string and 10 to "monkey" doesn't do the trick.
Actually, doing exactly that works just fine. But UnityScript does not share C#'s lower case "string" keyword alias, so in UnityScript, you have to write the actual object name, which is with a capitalized S, i.e. "String", not "string". This is the same code but for strings:
// Declaration of rectangular array:
var rectArray : String[,];
// Instantiate the rectangular array
rectArray = new String[3,3];
// Set the value of an element inside the array (at column = 2, row = 2) to "monkey"
rectArray[2,2] = "monkey";
// Access the element at that position again and store it in a local variable:
var twoPointTwo = rectArray[2,2];
Debug.Log("Value of element at 2,2: " + twoPointTwo);
Thank you very much for taking the time to answer my questions. Hopefully one day I will be good enough to help others as well the way you do!
Answer by Maya84 · Nov 13, 2014 at 02:50 PM
Thanks for the fast reply! however It still doesn't work for me. I tried your code inside of Unity and both Debug.Log's don't output what I want(number 2).
var arrayExample : int[];
arrayExample = new int[3];
arrayExample = new int[2];
var secondElement = arrayExample[1];
Debug.Log(secondElement);
Debug.Log(arrayExample[1]);
The classic javascript way allows me to acces single items of the array. (I'm sure that it can be done with your way two). But what I really want is to access data that is inside a array in a array(see my example on top) Hope this make sence!
You're fundamentally misunderstanding the syntax. In plain English, what you have in the above is:
A declaration of a variable (arrayExample) of type int array.
An instantiation of that variable to an int array with length 3.
A re-instantiation of that variable to a new int array, now with length 2. This overwrites the first instantiation.
Accessing the element at the second (zero-indexed) position. This number is 0, because you didn't assign any actual data to the array yet, you just declared their sizes.
Debug.Logs of this local variable, plus (again) the second element, which is still 0.
You need to read up on arrays here and fully understand their syntax before worrying about your question's actual topic which is called "rectangular arrays" (arrays of arrays):
Your answer
Follow this Question
Related Questions
ArrayLists giving wrong information? C# 2 Answers
get int based on 3 ints 0 Answers
Help with for loop and arrays 3 Answers
How to acces the value of List of arrays 0 Answers