- Home /
Is there a way to define arrays and objects literally (i.e. assign all values in one go, inline)?
Is there something comprable to this var myObj = {city: "NY", city:"LA"} or ["NY", "LA"]?
Answer by duck · Mar 09, 2010 at 11:05 AM
The Javascript that is built in to Unity doesn't support the JSON-style freeform objects. This is because it's not really Javascript, it's actually just a Javascript-like syntax laid on top of the Mono (.Net) engine.
This means that the types and classes available to you are basically (for the most part) the same as you have in C#.
So, there are a number of different types of container you can use to store data. You can read a whole lot about them at this page on the Unity Wiki:
Which Kind Of Array Or Collection Should I Use?
Now, specifically for your question - declaring and defining the values of an array inline - this is possible in Javascript.
For built-in arrays, you can do it javascript like this:
var myInts : int[] = [ 1,2,3,4,5 ];
var myStrings : String[] = [ "apple", "bear", "cloud", "diamond", "egg" ];
And in C# it's very similar:
int[] myInts = { 1, 2, 3, 4, 5 };
string[] myStrings = { "apple", "bear", "cloud", "diamond", "egg" };
If you want to use more flexible arrays, you can use the Unity's Javascript Array class, and it's possible to specify the values inline by passing in a built-in array as the parameter when you create it, like this:
var myArray = new Array( [1,2,3,4,5] );
// or
var myArray = new Array( [ "apple", "bear", "cloud" ] );
Or in C#, with an ArrayList (which is much the same thing)
ArrayList myArrayList = new ArrayList( { 1, 2, 3, 4, 5 } );
// or
ArrayList myArrayList = new ArrayList( { "apple", "bear", "cloud" } );
Now, if you really need a name-value pair data structure (eg, a Hashtable in either language, or a Dictionary is often a better choice in C#), I don't think there's a way to declare those inline. You basically have to create the object, and then add the name-value pairs line by line. For example hashtables in:
Javascript:
var myHashtable = new Hashtable();
myHashtable.Add( "apples", 8);
myHashtable.Add( "bananas", 6);
myHashtable.Add( "cakes", 3);
myHashtable.Add( "donuts", 20);
myHashtable.Add( "eggs", 12);
or C#:
Hashtable myHashtable = new Hashtable();
myHashtable.Add( "apples", 8);
myHashtable.Add( "bananas", 6);
myHashtable.Add( "cakes", 3);
myHashtable.Add( "donuts", 20);
myHashtable.Add( "eggs", 12);
Hope this helps!
Thank you very much! The psoudo JS has me confused all the time...
This may have been introduced in C# 3.0 (which you can use with Unity right now), but you can do this: dictionary = new Dictionary () { {5, "five"}, {10, "ten"}}
Answer by GlennHeckman · Aug 22, 2011 at 03:26 PM
Here's how I create a (Javascript) Hashtable with values directly in the same line (without the need for Add())
var myHash:Hashtable = {"key1":value1,
"key2":value2,
"key3":value3};
Enjoy!
Answer by faulknermano · Nov 20, 2015 at 11:33 AM
I'd like to add the following syntax for Generic Lists, similarly-looking to initialising the Javascript Array class:
var cth : List.<int> = new List.<int>( [5,40,45,49,53,56,59,62,65,68,70,72,74,76,78,80]);
Answer by Petr-Vejchoda · Jul 21, 2016 at 04:28 PM
As far as I know, Unity's .NET variation supports all versions of initializers. So you would do better on this page: https://msdn.microsoft.com/en-us/library/bb384062.aspx
Your answer
Follow this Question
Related Questions
instantiating multiple objects and assigning them individual values 2 Answers
Can't assign dictionary values 0 Answers
Mass assign shaders? 1 Answer
Assign Texture2D at runtime 2 Answers
How to force a variable to prefab 1 Answer