- Home /
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...
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.
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);
}
}
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...
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.
If you know what it was called, don't keep it to yourself. It might just be fine for my project.
Your answer
Follow this Question
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