• 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 mind 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Object reference not set to an instance of an object 1 Answer

JSON to object 0 Answers

JS to C# List type problem 1 Answer

Move object to raycast point. 3 Answers

javascript to C#, UnityEngine.Object to GameObject? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges