Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Jscript] How do I create a static 2D array in code?

Discussion in 'Scripting' started by Windexglow2, Jul 13, 2014.

  1. Windexglow2

    Windexglow2

    Joined:
    Sep 23, 2012
    Posts:
    90
    I understand how to set the variable up, but not how to have it have data.


    Code (jscript):
    1. static var testVar : Vector3[,];
    But I want it to have the variables already, like I could write

    Code (jscript):
    1.  static var testVar : Vector3[,] = new Vector3[,]{
    2.      new Vector3[]{
    3.         new Vector3(0,0,0),
    4.         new Vector3(0,1,1)
    5.     }
    6. }
    Now that bit of code won't work, but is there a similar way to do this?
    For sake of simplicity I say 2D, but I'm using a 3D array. Should be same logic to set up as 2D
     
  2. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    I don't even know what you're doing.
    Code (JavaScript):
    1. new Vector3[]{new Vector3(0,0,0), new Vector3(0,1,1)}
    For one, this is creating and filling a 1D array. The fact that you are using this inside of creating another array seems like you are copying the creation of a jagged array, but those are declared like [ ][ ], with two sets of square brackets without the comma.

    Generally speaking, most of the time you should/will define the size of the array (by going new int[3,5] for a matrix of three elements by five), then iterating through it to assign values.
     
  3. Windexglow2

    Windexglow2

    Joined:
    Sep 23, 2012
    Posts:
    90
    You got me in the right direction, thank you. On military deployment so web pages take a few minutes to load - finding questions and answers is hard when I only have an hour each night to myself.

    Here is a working example, which suits my needs.

    Code (jscript):
    1.  
    2. static var testVar =
    3.    [   //First bracket
    4.      [ //Second
    5.        [   //Third
    6.          //Fourth
    7.          [new Vector3(1,0,0), new Vector3(1,0,0)],
    8.          [new Vector3(0,0,0)],
    9.          [new Vector3(0,0,0)],
    10.          [new Vector3(0,0,0)]
    11.        ],
    12.        [   //1
    13.          [new Vector3(0,0,0)],
    14.          [new Vector3(0,0,0)],
    15.          [new Vector3(0,0,0)],
    16.          [new Vector3(0,0,0)]
    17.        ]
    18.      ],
    19.      [ //1
    20.        [   //0
    21.          [new Vector3(0,0,0)],
    22.          [new Vector3(0,0,0)],
    23.          [new Vector3(0,0,0)],
    24.          [new Vector3(0,0,0)]
    25.        ],
    26.        [   //1
    27.          [new Vector3(0,0,0)],
    28.          [new Vector3(0,0,0)],
    29.          [new Vector3(0,0,0)],
    30.          [new Vector3(0,0,0)]
    31.        ]
    32.      ]
    33.    ];
    34.    
    35. function Update(){
    36.    Debug.Log("Name : " + testVar[0][0][0][0]); //Returns Vector3(1,0,0)
    37. }
    38.  
     
  4. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    You're on the right track. It's definitely not best practices, but you will probably learn that lesson the fun way. Just don't make this too complicated so that you can still wrap your head around it.