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
0
Question by sophiadaniels · Jan 06, 2012 at 04:50 AM · javascriptobjectsjson

javascript objects/json question

i want to create an array that contains simple objects. but i can't figure out how to create simple objects.

if this was actionscript i'd do this: var spot:Object = new Object(); spot.open = true; spot.position = new Vector2(x,y);

or normally in javascript i'd just do this: var spot = {open:true, position: new Vector2(x,y)};

how do i do this in unity's javascript?

Comment
Add comment · Show 1
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 sophiadaniels · Jan 06, 2012 at 05:56 AM 0
Share

i was able to work around this using Hashtable ins$$anonymous$$d. spot["open"] is just as useful as spot.open for what i'm doing. as long as hashtables can contain child hashtables i should be set. still... foo.bar.lunch is nicer to type than foo["bar"]["lunch"]

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by by0log1c · Jan 06, 2012 at 06:12 AM

The Object class you're creating is - AFAIK - nothing but a base class to extends from, useful as a common base for pretty much every Unity objects but nothing in itself. What you want to do is probably create your own class that derives from it and add the variables you need to the new class/type. Then create an object of MyClass instead of Object. Using JS, its as simple as adding:

 public class MyClass
 {
     public var myString:String = "";
     public var myInt:int = "";
 
     //custom constructor
     public MyClass(someInt:int)
     {
         myString = "default";
         myInt = someInt;
     }
 }
 
 var myObject:MyClass = new MyClass(1337);
 myObject.myString = "neat";

You might want to take a look at Newbie Javascript Guide as UnityScript is not quite true JavaScript.

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 sophiadaniels · Jan 06, 2012 at 06:25 AM 0
Share

this is what i ended up using. it works for what i'm trying to do. i wanted a simple object that i could dynamicly add properties to. though i can see this evolving into a class as i flesh out the game.

 spots = new Array ();
 var r = 0;
 var c = 0;
 for(var i = 0; i < 9; i++){
     var spot = new Hashtable(); 
     spot["open"] = true;
     spot["position"] = new Vector2(c*110-150, r*110-150);
     c++;
     if(c >= 3){
         c = 0;
         r++;
     }
     spots.push(spot);
 }

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

How do i best describe an object with modular components in Json or other text? 1 Answer

Accessing JSON in unityscript 0 Answers

Editing avatars using node.js 0 Answers

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

Application.ExternalCall Issue 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