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 Tudor · Oct 05, 2012 at 09:19 PM · javascriptobjectattributeassignment

Creating object properties dynamically in unity javascript.

I am new to "unity Script", which seems to actually be fake javascript, and I'm not sure just how strict it is.

In plain old "nothing's sacred" Javascript, you can have, for instance:

 var canvas;
 canvas = document.getElementById("cvs");
 canvas.my_New_Attribute_Out_Of_Thin_Air_Because_Javascript = { "and" : 1,
                                                                "this" : 2,
                                                                "was" : "awesome"
                                                              };

Now it has come to my attention that this poop don't fly with unityScript...

My question is, is there any similarly convenient way to assign some attributes to some object?

The reason I wanted this behaviour, is that I have a large project with objects like "special_box : GameObject" already used in various places/ways, and it would be annoying to turn that into a data structure which contains "special_box" and other properties...

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 whydoidoit · Oct 06, 2012 at 02:09 PM 0
Share

It would be fair to say that Unity Script is more like Action Script than Java Script. Behind the scenes it is a .NET language that is compiled. So adding extra functions is a no-no etc. You can make use of compiler tricks which use hashtables to support some of the functionality - but whatever you do you need to bear in $$anonymous$$d it's not Javascript! Certain techniques may be slower etc.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by bamboo · Oct 06, 2012 at 02:03 PM

You can enable that behavior through the pragma expando directive. In the example below both properties.message and aGameObject.myProperties are dynamic properties.

 #pragma expando
 
 var aGameObject: GameObject;
 
 function Start () {
     
     var properties = new Object();
     properties.message = 'Look, Ma! A dynamic property!';
     
     aGameObject.myProperties = properties;
     
     Go();
 }
 
 function Go () {
     while (true) {
         yield WaitForSeconds(3);
         Debug.Log(aGameObject.myProperties.message);
     }
 }
 
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 Tudor · Oct 06, 2012 at 02:53 PM 0
Share

That's the stuff! I promise I will use it wisely :) (as in, not for serious projects). Also, this directive sounds like a harry potter spell...

avatar image
0

Answer by DaveA · Oct 05, 2012 at 10:54 PM

It seems I heard of someone that made a plugin to do that kind of thing, but I would recommend against it, if you care about performance.

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 Tudor · Oct 06, 2012 at 12:07 PM 0
Share

If you know what it was called, don't keep it to yourself. It might just be fine for my project.

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

12 People are following this question.

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

Related Questions

Disable GameObject Only Father Not Children 2 Answers

Setting a variable to an instantiated object. 1 Answer

Drawing a 3D object javascipt 0 Answers

Object won't collide with Player unless Player is moving 1 Answer

Rotating an Object on Key Press! 3 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