Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Newbie Question : Arrays

Discussion in 'Scripting' started by FirebladeBR, Dec 1, 2014.

  1. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Hi Guys,

    I have been searching the forum and the internet but I still did not manage to make my multidimensional array to work. Here's My code:

    Code (JavaScript):
    1. var e = new Array();
    2.  
    3. function test1(){
    4.  
    5. var current_e = e.length;
    6.  
    7. e[current_e][0] = "a";
    8. e[current_e][1] = "b";
    9. e[current_e][2] = "c";
    10.  
    11. }
    What's wrong with this code?

    I get the following error: BCE0048: Type 'Object' does not support slicing.

    Thanks,
     
  2. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    on line 5 you're creating an integer
    var current_e = e.length;
    'var' creates a variable that takes the implicit type of whatever value it's being given, and since Array.length returns an integer, that means current_e is an integer. the value of that integer is just how long the array is. let's say you have 5 items in the array, then current_e is an integer holding the value 5.

    e[current_e] just references the last item in the array, because you're passing in its length as the index. basically it's the same as e[5] if e has 5 items in it.

    now the syntax e[x][x] is used only when e is a 2-dimensional array, which i don't know if it is or not but i'm assuming it is only a 1-dimensional array, where you would simply use e[x] to refer to any item in it.

    so if you want to go through all the items in the array, you don't put e[current_e][0] etc. you just put e[0], e[1], etc.

    in fact the name 'current_e' is misleading because it isn't the current index, it's the last index, since you gave it the value of the length of e.

    to go through a list of items in an array automatically, you can use a for-loop, or a foreach-loop, depending on your needs.

    here's a for-loop:
    Code (csharp):
    1. for(i=0; i < e.length; i++)
    2. {
    3.     e[i] = "whatever";
    4. }
    5. //this code just sets the value of all items in e to "whatever"
    here's a foreach-loop
    Code (csharp):
    1. foreach(string s in e)
    2. {
    3.     s = "whatever";
    4. }
    5. //this code does the same thing but without access to the index, i
    since your example sets the items of e to a, b, c, etc. you probably should use the for-loop since that gives you a token ( i ) and you can use that to do increments with (i forget how to change a number to a letter of the alphabet but i know it can be done easily).

    foreach just lets you execute the same code to all elements of the specified type in an enumeration. this, to my knowledge, doesn't really give you easy access within the loop to which index or item you're working on, so its use cases aren't the same as for-loops.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That'll actually give you an index out of bounds. The last index in a 5 element array is at 4, not 5, because they start at 0. :)
     
  4. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Yes, I am trying to make a 2 dimensional array. I need to store 4 informations for each index in the array.

    [x][0] -> id
    [x][1] -> name
    [x][2] -> gender
    [x][3] -> texture_name

    So i want to cycle trough this array so I can know each information.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Never use the JS Array class. See here for 2D arrays and more.

    --Eric
     
  6. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Eric,

    I read the link you send. But I still have one doubt, how do you declare a jagged array but with no fixed "width" and "height.

    I wanted an Array wich I could just endlessly add rows.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    A List of Lists would work. e.g.,

    Code (javascript):
    1. var x = new List.< List.<String> >();
    2. x.Add (new List.<String>());
    3. x[0].Add ("Whee!");
    4. print (x[0][0]);
    --Eric
     
  8. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    I just copied your code and got the following error:

    BCE0018: The name 'List' does not denote a valid type ('not found'). Did you mean 'UnityEngine.Light'?

    Do I need to import something?

    Thanks
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    See the link I posted above about using generic Lists.

    --Eric
     
  10. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    This part is what I found usefull:

    But in this example it only shows a fixed array ize: like 32x32 and 16x4. What I am not getting is how to make it an array with unlimeted ammount of indexes. Something like = new Array([,]).
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    What I already posted about using a List of Lists...just do that. Arrays are fixed-size.

    --Eric
     
  12. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    I tryed that and it keeps telling me that "The name 'List' does not denote a valid type ('not found')."

    I even tryed to copy and paste other codes in the link about lists and it trows the same error:

    Am I missing an import here?
     
  13. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    I found what was missing. You have to import:

    import System.Collections.Generic;