Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Joeri · Mar 27, 2011 at 03:57 PM · javascriptgriduce0001bce0022multidimarray

How to declare and initialize multidimensional arrays JS?

I'm trying to instantiate a grid using cubes with this shape

alt text

I'm trying to use a multidimensional array for this but I have problems initializing the array with set values.

public var multiDimensionalArray: int[,] =  [
[0,1,1,0],
[1,0,0,1],
[1,0,0,1],
[0,1,1,0]
];

Above is the first method I've tried but it throws the following error

Assets/TetrisPiece.js(7,45): BCE0022: Cannot convert 'int[][]' to 'int[,]'.

I've also tried storing an array in a array

var myArray: int[4];
myArray[0] = new int[0,1,1,0];
myArray[1] = new int[1,0,0,1];
myArray[2] = new int[1,0,0,1];
myArray[3] = new int[0,1,1,0];

But this shows the following error:

Assets/TetrisPiece.js(1,17): UCE0001: ';' expected. Insert a semicolon at the end.

I suspect a syntax error but I haven't been able to find what. Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Eric5h5 · Mar 27, 2011 at 05:28 PM

Declaring an array the way you did initially results in a jagged array. If you leave off the type, type inference will type it as a jagged array automatically:

public var multiDimensionalArray = [
[0,1,1,0],
[1,0,0,1],
[1,0,0,1],
[0,1,1,0]
];

(The type for this is int[][], not Array.) Then you can do

multiDimensionalArray[0][1]

instead of

multiDimensionalArray[0,1]

Jagged arrays aren't quite the same thing as multidimensional arrays, but it would work well enough. If there's an easy syntax to initialize multidimensional arrays in JS by filling them with initial values, I don't know what it is. If you really need a "proper" multidimensional array, you might have to do it the hard way:

public var multiDimensionalArray : int[,];
multiDimensionalArray[0,0] = 0;
multiDimensionalArray[0,1] = 1;
...
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joeri · Mar 27, 2011 at 05:45 PM 0
Share

Thanks. This at least got me as far as that I am able to replicate my image with cubes. So type inferencing will make it a jagged array. But if I set the type as int[][] I get an error. So I can't set the type myself?

avatar image Eric5h5 · Mar 27, 2011 at 06:03 PM 0
Share

For jagged arrays you can't explicitly set the type yourself in JS, which is an oversight, but it's irrelevant whether the type is derived from type inferencing or explicitly stating it--the compiled code is exactly the same either way. Note that type inferencing has nothing to do with dynamic typing, which is a different subject entirely, if that's what you're concerned about.

avatar image Fattie · Jul 19, 2012 at 04:27 PM 0
Share

a somewhat similar problem .. ?

http://answers.unity3d.com/questions/286682/2d-array-persists-during-multiple-game-sessions.html

avatar image joserico · Aug 24, 2015 at 02:19 PM 0
Share

And what about to push other array to index like

 multiDimensionalArray[4] = [1,0,1,0];

this code get the mistake "Array index is out of range."

avatar image
3

Answer by dentedpixel · Nov 20, 2012 at 05:12 PM

 public var multiDimensionalArray: int[,] = new int[4,4];
 
 multiDimensionalArray[0,0] = 1;
 multiDimensionalArray[0,1] = 1;
 multiDimensionalArray[0,2] = 0;
 multiDimensionalArray[0,3] = 1;
 multiDimensionalArray[1,0] = 0;
 ....
 multiDimensionalArray[3,3] = 0;


You can also turn this into a for loop to set the values.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zarenityx · Jan 27, 2013 at 10:59 PM 0
Share

Thanks! in my case I could not use either jagged arrays, nor Array or List classes, and it kept telling me I needed a semicolon. On the other hand, if I just said boolean[,], not all of my GUI would show up and none of the script would work. I never thought of this. Thanks!

avatar image
1

Answer by mani444 · Aug 17, 2014 at 11:07 AM

I am trying this for making grid with each index containing card object but its not working. Same error.

function Start () {

 var i:int = 0;
 var j:int = 0;
 aCards = new Array(); //Just Ignore it
 aGrid = new Array();
 aCardsFlipped = new ArrayList(); //Just Ignore it
 for(i=0; i<4; i++)
 {
     aGrid[i] = new Array();
     for(j=0; j<4; j++)
     {
         aGrid[i][j] = new Card(); //Assigning object of class card to grid
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Meltdown · Mar 27, 2011 at 04:05 PM

JavaScript doesn't actually have support for multi-dimensional arrays, but you can nest arrays. For most programming purposes, nested arrays are equivalent to multi-dimensional arrays. Therefore we are not going to worry about the differences between them here.

To create a nested array, you just define an array element as being an array.

var outerA = new Array();
outerA[0] = new Array();
outerA[1] = new Array();
outerA[2] = new Array();

When using array literals, the same effect can be achieved by nesting square brackets.

var aName = [[1,2,3],['a','b','c']];

// is the same as

var aName = new Array(); aName[0] = [1,2,3]; aName[1] = ['a','b','c'];

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joeri · Mar 27, 2011 at 04:31 PM 0
Share

Thanks a lot for the fast reply. I read that 3.2 added the support for multidimensional arrays. I have been able to use it to instantiate a grid of cubes using a nested for loop. Just haven't been able to initialize it with set values.

I see your using the javascript Array class in this example. How could I use this with build in arrays and what's the syntax for this? I read those are much faster then javascript arrays.

avatar image Eric5h5 · Mar 27, 2011 at 05:15 PM 0
Share

JS does have support for multidimensional arrays. There's very little reason to ever use the Array class; built-in arrays or generic Lists are almost always a better idea.

avatar image Meltdown · Mar 27, 2011 at 05:24 PM 0
Share

$$anonymous$$y bad :-( Guess my JS knowledge is a bit out of date then.

avatar image Joeri · Mar 27, 2011 at 05:29 PM 0
Share

That's why I asked. Regardless though I'm having trouble accessing the arrays in this example.

var outerA = [ [0,1,1,0], [1,0,0,1], [1,0,0,1], [0,1,1,0] ];

How would I go about accessing this in a for loop?

avatar image Eric5h5 · Mar 27, 2011 at 05:44 PM 0
Share

Just to be nitpicky, JS always had support for multi-d arrays, but it wasn't possible to declare the type explicitly until Unity 3.2. This meant it was possible to use various workarounds involving type inference; see here: http://www.unifycommunity.com/wiki/index.php?title=Javascript$$anonymous$$ultiDimArrays

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Resizable grid of gameobjects 1 Answer

js(9,31): UCE0001: ';' expected. Insert a semicolon at the end. 2 Answers

Instantiate prefab on a grid pattern? 1 Answer

; expected. Insert a semicolon... C# & JavaScript 2D Controller 0 Answers

I need help with an error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges