Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
5
Question by Ashkan_gc · Feb 22, 2010 at 06:14 PM · javascriptarrays

how to create multidimensional arrays in javascript

we can create arrays in javascript with a notation like

var a : float[];

but i can not create multidimensional arrays in javascript like

var a : float[,];

how can i create a two dimensional array in javascript? i want .NET arrays and don't want array class arrays.

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 · Nov 30, 2011 at 08:09 AM

This question has been moot since Unity 3.2. In fact you can do

 var a : float[,];

with no problem.

Comment
Add comment · Show 6 · 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 Ayelis · Mar 31, 2018 at 12:18 AM 0
Share

Well, as of Unity 2017.1.1f1, BCE0077: It is not possible to invoke an expression of type 'float[,]'. so... Try again.

avatar image Bunny83 Ayelis · Mar 31, 2018 at 12:31 AM 1
Share

UnityScript is a dead language. It's no longer maintained, it's no longer officially supported and will be completely removed in future Unity versions so you shouldn't really bother. Though this still works fine:

 // UnityScript
 var a : float[,];
 
 function Start () {
     a = new float[3,2];
 }


avatar image Ayelis Bunny83 · Mar 31, 2018 at 12:49 AM 0
Share

Thanks, that works great. Looks like I might have been doing it wrong. I had honestly tried searching the Unity $$anonymous$$anual and left some nice (as possible) feedback to the Unity $$anonymous$$m about how difficult it is to find the most basic things... And about how it takes 10 seconds to run a single search... Thus why I turned to googling 8 year old forum posts... >_>

avatar image Eric5h5 Ayelis · Mar 31, 2018 at 12:32 AM 0
Share

Yes, you do need to try again. The Unityscript compiler hasn't changed in years; the answer also has not changed, and will continue to be correct until they remove Unityscript entirely. You do realize that this question isn't about C#.

avatar image Ayelis Eric5h5 · Mar 31, 2018 at 12:43 AM 0
Share

I'm coding in JavaScript. That's the whole reason I came to a question with the title "How to ... in javascript". What more evidence can I give that I'm coding in JavaScript?

JavaScript error

js.png (15.2 kB)
Show more comments
avatar image
7

Answer by Eric5h5 · Feb 22, 2010 at 06:46 PM

While you can use multi-dimensional arrays in Javascript, the syntax for creating them is missing. Fortunately you can get around that by using type inference, as shown in this helper script.

Comment
Add comment · Show 6 · 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 Ashkan_gc · Feb 23, 2010 at 05:00 AM 0
Share

thank you eric! i don't use js myself but i want to help a friend to do this in javascript. i searched in mono's website and many other pages but nothing found. thank you again

avatar image Eric5h5 · Feb 23, 2010 at 06:20 AM 0
Share

Yeah, it's a Unity limitation. It should be possible to directly declare arrays the way you wrote, without using type inference work-arounds, but currently it's not. Hopefully it will be fixed sometime....

avatar image Ashkan_gc · Feb 23, 2010 at 07:41 AM 0
Share

mono's JS compiler is not an active part of the development. they might support js by DLR in the future but there is no plans about that too. take a look at this. if unity upgrade it's mono version then we can add support for scripting in our applications with DLR. mono supports DLR and we can easily make our games scriptable with IronPython or rubby. they'll upgrade it soon.

avatar image Eric5h5 · Feb 23, 2010 at 08:23 AM 0
Share

That's not really relevant. Unity's Javascript is unique to Unity, and is developed by the creator of Boo, Rodrigo Barreto de Oliveira, and uses a lot of code from the Boo implementation.

avatar image Ashkan_gc · Feb 26, 2010 at 01:41 PM 0
Share

really!? i don't think so. I'll ask it from support $$anonymous$$m.

Show more comments
avatar image
1

Answer by Ashkan_gc · Feb 23, 2010 at 07:47 AM

thank you guys for the answers. so unity's javascript compiler or mjs (mono jscript compiler) is not capable of declaring multiDimensional arrays but can use them. they are first class members of the .NET framework and are not language dependent so you can declare them in C# and return them and use them in JS. in fact when you declare an array in C# it will crate an instance of System.Array or one of it's children. the problem is js don't support this syntax for rectangular arrays but surely it can use the class. you should declare a static method for that for easier use.

static int[,] intArray(int d1,int d2)
{
   return new int[d1,d2];
}

you can define a function for each type of use reflection to create a generic method. generics themselves are not supported in js so you can not simply write

static t[,] arr<t>(int d1,int d2)
{
   return new t[d1,d2];
}

i mean you can write but in js you can not write

var t = arr<int>(10,30);

you can make a generic method using typeof operator and system.type class and ... remember if your array is an array of a reference type (a class) then you should populate all elements before returning it.

public static string[,] createString (int x,int y)
{
string s[,] = new string[x,y]; //this will declare an array of string in size x X y but all elements are null.
//you should populate them yourself by hand.
for (int i=0;i<x;i++)
for (int j=0;j<y;j++)
s[i,j]= "new string"; // for other types of classes you might use new classname();
//then you can return s
return s;
}
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
-1

Answer by matthew 2 · Nov 30, 2011 at 07:49 AM

You can just use this:

 var YourArrayHere = new Array();
 YourArrayHere.length = [first dimension array length here];
 for(var count = 0; count < YourArrayHere.length; count++)
 {
     var TempSecondArray = new Array();
     TempSecondArray.length = [sec dimension array length here];
     YourArrayHere[count] = TempSecondArray;
 }

And it can be accesed with

YourArrayHere[First dimension index][Second dimension index]

It may be long.. but this is the way i do it :)

Comment
Add comment · Show 2 · 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 syclamoth · Nov 30, 2011 at 07:50 AM 0
Share

The OP kind of specifically said that they didn't want this.

avatar image Eric5h5 · Nov 30, 2011 at 08:10 AM 0
Share

Also, you can just use multidimensional arrays. You really don't want to use the Array class.

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

Converting a JS script into C# - hit a wall with arrays... 1 Answer

Remove Items and Item Tooltips 0 Answers

How do I delete the elements in array? 1 Answer

Why does it give me this error and how can i fix it? 1 Answer

going through each item of a multi-dimensional array 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