Javascript: Arrays in array

Hi guys,

I have a few questions that need answer. Any help will be appreciated. Thanks in advance.

My game need me to use arrays inside another array, For example:

var a = new Array([1,2,3],[2,3,4],[2,3,4],[4,5,6],...);

1.How can I debug.log or print the whole array in a line?

  1. As you can see a[1] and a[2] above is repeated. What is the best way to compile it into unique object array?

Thanks!

Put it in the Update function :slight_smile:

  1. Enumerable.Distinct Method (System.Linq) | Microsoft Learn

^^this guy is using javascript, and yeah i am not sure what you are doing, use c# maybe, in python with no matplotlib you also have to use lists within lists, in c# you have 2d arrays for example:

int[,] Array = new int[10, 10];

consider c# handles data better is my 2 cents

in my code i do have one use for this, storing the whole array as a string, you can simply iterate the array adding commas, then you can load it back in by using the split function

you can test the size of the array with Array.GetLength(0) and Array.GetLength(1), i personally use a for loop rather than the foreach structure as i am language agnostic:

string sString = “”;
for (x = 0; x < Array.GetLength(0); x++)
{
for (y = 0; y < Array.GetLength(1); y++)

sString += Array[x,y] + “,”
}

Debug.Log(sString)

would result in the array outputted in the form 1,4,5,6,7,3,5, etc

good luck, i am not sure this is any use to you but that’s how it’d go in c# and i started thinking about it

That’s not relevant; all languages in Unity have access to .NET methods. Using Distinct as Jessy said would be the simplest.

The Array class really shouldn’t be used…use Lists or a 2D array. As for question 1, use a loop.

–Eric

i quote, “The Array class really shouldn’t be used…use Lists or a 2D array.”

so lol, don’t use array, use array!

haha anymore helpful advice? xD

Erm, I said don’t use the Array class, use a 2D array. As in:

var myArray : int[10, 3];

The Array class is slow and untyped, so don’t use it. Programming has very specific terminology: you’ll find you make much better progress if you learn it. Hopefully that’s helpful enough for you.

–Eric

what is an “array class” then?

if you read my code it used a 2d array:

int[,] Array = new int[10, 10];

What the code in the first post is using. You could also just look it up in the docs.

I wasn’t referring to your code, which should have been obvious since I specifically quoted the code in the first post.

–Eric

oh i see fair enough, i thought you where quoting me

well i learnt something about the specific implementation of javascript in unity then

Found this thread in a google search but couldn’t get that line to work. The following worked though so just updating for others to see…

var myArray = new int[10, 3];

The one Eric did is in Unity’s JS. The one you used looks like is in C#.

Gah, did I write that? What a dummy. :wink: What I meant was:

var myArray : int[,];

Which you could then initialize later as you specified:

myArray = new int[10, 3];

@wolfhunter777: no, Essential is quite correct, and has written Unityscript. Though it would also work as C#, as long as it was inside a function.

–Eric

I was switching tabs between your code here and from another post of yours from two years ago saying not to write exactly what you wrote above, hehe. Had me confused for a while. :wink:

I never knew that. Then again, I have never coded Unity’s JS that much. Good to know though.