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
6
Question by davedev · Mar 09, 2010 at 10:03 AM · valuesassignbuiltin arrayinline

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"]?

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
15
Best Answer

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!

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 davedev · Mar 09, 2010 at 11:46 AM 0
Share

Yes. This great. Thanks.

avatar image Will 12 · Sep 02, 2010 at 03:24 PM 0
Share

Thank you very much! The psoudo JS has me confused all the time...

avatar image Matt 12 · Sep 02, 2010 at 04:03 PM 0
Share

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"}}

avatar image Eric5h5 · Sep 02, 2010 at 08:26 PM 0
Share

Don't post comments as answers please.

avatar image Aldwoni_legacy · May 04, 2011 at 11:31 AM 0
Share

If you use C# you can also use Lists.

avatar image
0

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!

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 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]);

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 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

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

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

3 People are following this question.

avatar image avatar image avatar image

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


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