c# - Populating an inventory UI with JSON, what's wrong with my code? -


i'm using json , litjson in unity 5 populate inventory ui game. problem is, code not returning except information item @ id:0. @ point have 2 items in json. code in unity recognize there items @ id:0 , id:1, because if put else in there, error outside of argument, it's still printing information console id:0.

here json:

    [     {         "id": 0,         "title": "stun gun",         "value": 6,         "stats": {             "power": 100,             "defense": 4,             "vitality": 2         },         "description": "testing stun gun.",         "stackable": false,         "rarity": 2,         "slug": "stun_gun"     },      {     "id": 1,         "title": "the great gun",         "value": 500,         "stats": {             "power": 700,             "defense": 10,             "vitality": 10         },         "description": "the great gun great.",         "stackable": true,         "rarity": 6,         "slug": "the_great_gun"     } ] 

here inventory script:

using unityengine; using unityengine.ui; using system.collections; using system.collections.generic;  public class inventory : monobehaviour {     gameobject inventorypanel;     gameobject slotpanel;     itemdatabase database;     public gameobject inventoryslot;     public gameobject inventoryitem;      int slotamount;     public list<item> items = new list<item>();     public list<gameobject> slots = new list<gameobject>();      void start ()     {         database = getcomponent<itemdatabase>();          slotamount = 8;         inventorypanel = gameobject.find ("inventory panel");         slotpanel = inventorypanel.transform.findchild ("slot panel").gameobject;         (int = 0; < slotamount; i++)          {             items.add(new item());             slots.add(instantiate(inventoryslot));             slots[i].transform.setparent(slotpanel.transform);         }          additem(0);         additem(1);          debug.log(items[1].slug);     }      public void additem (int id)     {         item itemtoadd = database.fetchitembyid(id);         (int = 0; < items.count; i++)          {             if (items[i].id == -1)             {                 items[i] = itemtoadd;                 gameobject itemobj = instantiate(inventoryitem);                 itemobj.transform.setparent(slots[i].transform);                 itemobj.transform.position = vector2.zero;                 itemobj.getcomponent<image>().sprite = itemtoadd.sprite;                 itemobj.name = itemtoadd.title;                  break;             }         }     } } 

and here itemdatabase script:

using unityengine; using system.collections; using litjson; using system.collections.generic;  using system.io;  public class itemdatabase : monobehaviour {     private list<item> database = new list<item>();     private jsondata itemdata;      void start ()     {         itemdata = jsonmapper.toobject(file.readalltext(application.datapath + "/streamingassets/items.json"));         constructitemdatabase();          debug.log (fetchitembyid(0).description);     }      public item fetchitembyid (int id)     {         (int = 0; < database.count; i++)             if(database[id].id == id)                 return database[i];         return null;     }      void constructitemdatabase ()     {         (int = 0; < itemdata.count; i++)          {             database.add(new item((int)itemdata[i]["id"], itemdata[i]["title"].tostring(), (int)itemdata[i]["value"],                 (int)itemdata[i]["stats"]["power"], (int)itemdata[i]["stats"]["defense"], (int)itemdata[i]["stats"]["vitality"],                  itemdata[i]["description"].tostring(), (bool)itemdata[i]["stackable"], (int)itemdata[i]["rarity"],                  itemdata[i]["slug"].tostring()));         }     } }  public class item {     public int id { get; set; }     public string title { get; set; }     public int value { get; set; }      public int power { get; set; }     public int defense { get; set; }     public int vitality { get; set; }     public string description { get; set; }     public bool stackable { get; set; }     public int rarity { get; set; }     public string slug { get; set; }     public sprite sprite { get; set; }      public item (int id, string title, int value, int power, int defense, int vitality, string description, bool stackable, int rarity, string slug)     {         this.id = id;         this.title = title;         this.value = value;         this.power = power;         this.vitality = vitality;         this.description = description;         this.stackable = stackable;         this.rarity = rarity;         this.slug = slug;         this.sprite = resources.load<sprite>("sprites/items/" + slug);     }      public item ()     {         this.id = -1;     } } 

i'm getting correct number of slots, , correct number of filled versus empty slots (2), both slots filled "stun_gun". appreciated. i'm new unity/c#.

shouldn't be

public item fetchitembyid (int id) {     (int = 0; < database.count; i++)         if(database[i].id == id)  // rather id             return database[i];     return null; } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -