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…

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);
	}
}

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.