unity-game-engineunityscript

Does UnityScript support Object Literal syntax?


I hear that UnityScript is pretty close to JavaScript. Does UnityScript support the Object Literal syntax of JavaScript?

e.g.

var x = {
    y: 12
};

Strangely, searching google for UnityScript "Object Literal" yields no useful results.


Solution

  • UnityScript versus JavaScript have very different semantics.

    Creating a new JavaScript file in Unity with this syntax will not compile.

    #pragma strict
    
    var x = {
        y: 12 // error: BCE0005
    };
    
    function Start () {
       Debug.Log (x.y); // error: BCE0019
    }
    

    This will give build errors:

    NewBehaviourScript(4,5): BCE0005: unknown identifier: 'y'.
    NewBehaviourScript(8,17): BCE0019: 'y' is not a member of 'Boo.Lang.Hash'.

    However, you could implement a Hashtable:

    #pragma strict
    
    var x:Hashtable = new Hashtable();
    x["y"] = 12;
    
    function Start () {
       Debug.Log (x["y"]);
    }