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

Filling in an inventory list.

Discussion in 'Scripting' started by Morokiane, Mar 12, 2015.

  1. Morokiane

    Morokiane

    Joined:
    May 29, 2013
    Posts:
    3
    I want to make an inventory that is in a style of a list that displays various information to the player. However, I'm stuck trying to fill in the fields of the button that represents the inventory item. So far I have two scripts, the InventoryList.cs that takes information from the ItemDatabase.cs (right now only in the inspector) and fills in the fields on a custom button. What I can't figure out is how to get the items in the database to fill in from code and not just from the inspector.

    InventoryList.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class InventoryList : MonoBehaviour {
    8.  
    9.     public GameObject inventoryButton;
    10.  
    11.     public List<Item> inventory = new List<Item>();
    12.     ItemDatabase database;
    13.  
    14.     public Transform contentPanel;
    15.  
    16.     void Start () {
    17.         PopulateInventory ();
    18.     }
    19.  
    20.     void PopulateInventory(){
    21.  
    22.         database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<ItemDatabase> ();
    23.  
    24.         foreach (var item in inventory) {
    25.             GameObject newInventory = Instantiate (inventoryButton) as GameObject;
    26.             InventoryItem iButton = newInventory.GetComponent <InventoryItem> ();
    27.             iButton.WeaponName.text = item.itemName;
    28.             iButton.WeaponType.text = item.itemType.ToString ();
    29.             iButton.WeaponLevel.text = item.itemLevel.ToString ();
    30.             iButton.UpgradeCost.text = item.itemUpgradeCost.ToString ();
    31.             iButton.ScrapCost.text = item.itemScrapCost.ToString ();
    32.             iButton.maxIcon.SetActive (item.itemIsMaxLevel);
    33.             iButton.Icon.sprite = item.itemIcon;
    34.             newInventory.transform.SetParent (contentPanel, false);
    35.         }
    36.     }
    37. }

    ItemDatabase.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [System.Serializable]
    6. public class Item {
    7.  
    8.     public GameObject weapon;
    9.     public string itemName;
    10.     public int itemID;
    11.     public int itemUpgradeCost;
    12.     public int itemScrapCost;
    13.     public int itemLevel;
    14.     public Sprite itemIcon;
    15.     public bool itemIsMaxLevel;
    16.     public ItemType itemType;
    17.    
    18.     public enum ItemType {
    19.         Primary, Secondary, Consumable
    20.     }
    21. }
    22.  
    23. public class ItemDatabase : MonoBehaviour {
    24.  
    25.     public List<Item> itemDatabase;
    26.  
    27.     void Start() {
    28.         itemDatabase = new List<Item> () {
    29.             new Item() {
    30.                 itemName = "Sword",
    31.                 itemID = 0,
    32.                 itemUpgradeCost = 23,
    33.                 itemScrapCost = 10,
    34.                 itemLevel = 1,
    35.                 itemIsMaxLevel = false,
    36.                     //... and so on
    37.             },
    38.             new Item() {
    39.                 itemName = "Shield",
    40.                 itemID = 1,
    41.                 itemUpgradeCost = 12
    42.                     //... and so on
    43.             }
    44.             //... and so on
    45.         };
    46.     }
    47.     //... and so on
    48. }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    It looks to me like your code is already taking information from the ItemDatabase and using it to create buttons. Can you explain what happens when you run this code, and how that differs from what you want to happen?
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Oh, wait, I should have looked more carefully at the second code listing, which I think is just pseudo-code for what you actually want. Your question is, what's the valid syntax for that pseudocode?

    It'd be something like this:

    Code (CSharp):
    1. itemDatabase = new List<Item>();
    2. Item item = new Item();
    3. item.itemName = "Sword";
    4. item.itemID = 0;
    5. item.itemUpgradeCost = 23;
    6. // ...etc...
    7. itemDatabase.Add(item);
    8.  
    9. item = new Item();
    10. item.itemName = "Shield";
    11. item.itemID = 1;
    12. item.itemUpgradeCost = 12;
    13. // ...etc...
    14. itemDatabase.Add(item);
    15.  
    16. // And so on!
    Note that this isn't how I would actually recommend doing it — you'll be much happier defining these things as data somewhere, reading from a text file or using Google Sheets or some such. But if you want to do it as code, that's how: create each item, initialize it, and then Add it to the list.
     
  4. Morokiane

    Morokiane

    Joined:
    May 29, 2013
    Posts:
    3
    Unity is telling me the InventoryList.cs that the database is assigned but not used. I'm guessing in the iButton section, "iButton.WeaponName.text = item.itemName;" I need to direct it to it?

    I've had a ton of trouble getting this to work, would external data file be a nightmare?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Well, I guess it's right. Delete the "inventory" declaration on line 11. You don't want InventoryList to have its own inventory; you want it to use the one in the database, right? So then on line 24 (which will now be line 23 or so), you should iterate over database.itemDatabase (instead of the old "inventory" list which you just deleted).

    And yeah, reading the data from a file is going to be more complex than this. So, you should probably stick to this straightforward approach for a while.