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
1
Question by Comaleaf · Apr 19, 2012 at 07:29 PM · javascript

UnityScript equivalent of JavaScript objects

In JavaScript you can use objects as simple dictionaries, and there is a concise syntax for expressing object literals.

E.g.

 var someLookUp = {
     foo: 2,
     bar: {
         a: 1,
         b: 2,
         c: 3
     }
 };
 
 someLookUp['bar']['b']; // 2

How can I do these kinds of things with UnityScript?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Apr 19, 2012 at 08:01 PM

Maybe you're looking for classes and/or structs?

 class Bar extends System.ValueType { // struct
     var a : int;
     var b : int;
     var c : int;
     function Bar (a : int, b : int, c : int) {
         this.a = a; this.b = b; this.c = c;
     }
 }

 class Something {
     var foo : int;
     var bar : Bar;
     function Something (foo : int, bar : Bar) {
         this.foo = foo; this.bar = bar;
     }
 }
 // ...

 var lookup = new Something (2, Bar(1, 2, 3));
 print (lookup.bar.b);
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 Comaleaf · Apr 19, 2012 at 08:07 PM 0
Share

I had not encountered System.ValueType and this is useful to know, however what if I wanted to look up an object member by key? You example uses lookup.bar.b but is there a way to access these, or an alternative, whereby a variable key can be used e.g. if my example had been var key = 'foo'; someLookUp[key]; // 2

avatar image
1

Answer by Mr Brent · Oct 24, 2013 at 03:52 AM

I realize this question is old as dirt, but what you're looking for are Hashtables.

http://docs.unity3d.com/412/Documentation/ScriptReference/Hashtable.html

Comment
Add comment · Show 8 · 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 Eric5h5 · Oct 24, 2013 at 03:58 AM 0
Share

Generic Dictionaries, rather. Dictionary does everything Hashtable does, except better/faster/etc.

avatar image Mr Brent · Oct 24, 2013 at 04:02 AM 0
Share

They aren't really equivalents of JOs, as you have to define the type of variables inside, where you don't with a Hashtable. If you have a single type, then yes, go for it, Dictionaries are better.

avatar image Eric5h5 · Oct 24, 2013 at 04:22 AM 2
Share

You don't have to define the type, or rather you can use Object as the type, which amounts to the same thing. It's rare that you'd actually do that however; normally you use a consistent type. The reasons you shouldn't use Hashtable are 1) Dictionary has more functionality and is faster, 2) since Hashtable is untyped, you need to cast values when retrieving them (which also applies to Dictionary if you use Object as the type), 3) if you care about WP8 or Windows store publishing, you can't use obsolete collections such as ArrayList and Hashtable, since they're not supported on those platforms.

A Hashtable isn't a direct equivalent to a Javascript object either, so it's best avoided considering the downsides.

avatar image Romano · Mar 04, 2014 at 06:49 AM 1
Share

Thankyou so much, you've just saved me a massive ballache. Why in the name of god are unity putting this in their own tutorials? This is what it says:

 //JavaScript does not have a List or Dictionary in the same
 //way that C# does. Ins$$anonymous$$d, JavaScript has either non-typed
 //'Objects' or an 'Array' object. You can acheive the same
 //level of functionality using these, however the implementation
 //is too different from this lesson to include here.
avatar image Eric5h5 · Mar 04, 2014 at 07:01 AM 1
Share

Yeah, that's just...wrong. I've submitted a bug report; it took maybe 2 $$anonymous$$utes to convert the scripts from C# to JS. (And in no way should they be recommending non-typed Objects or Array. That's web Javascript, not Unity.)

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Closure does not work in Unity JS functions? 1 Answer

Collide detection with tag [JS] 0 Answers

What should i learn for unity C# or UnityScript? 4 Answers

Instantiate position from four options. 1 Answer

"Object reference not set to an instance of an object" when attempting to parse Dictionary of Lists with JSONFx, MiniJSON 0 Answers


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