Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Javascript Object {}

Discussion in 'Scripting' started by Hapciupalit, Jan 14, 2016.

  1. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    I'm trying to make some javascript object for my InGameShop and got some error and can't find how to fix it.
    So my code is :
    Code (JavaScript):
    1. var object1 = {
    2.     name: "Object 1",
    3.     price: 20,
    4.         damage: 10
    5. };
    What it's not ok? Or the object it's not supported?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    unity's javascript is NOT javascript... it's a really bad misnomer because the language is syntactically similarish to javascript with many exceptions.

    Really the language is 'unityscript', and it compiles to mono/.net CIL. This means it must meet the standards of mono/.net CIL. This includes no support for dynamic objects like that.

    Instead what you would do is define the custom class and use that. All types must be explicitly defined, members and all.

    Code (csharp):
    1.  
    2. public class ShopItem
    3. {
    4.     public var name:string;
    5.     public var price:float;
    6.     public var damage:float;
    7. }
    8.  
    9.  
    10. var obj = new ShopItem();
    11. obj.name = "Object 1";
    12. obj.price = 20f;
    13. obj.damage = 10f;
    14.  
     
    Hapciupalit likes this.
  3. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    Thank you very much !