Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Morokiane · Mar 11, 2015 at 05:34 PM · inventorylistsitems

Filling in an inventory list.

I want to make a 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 field of the button that represents the inventory item. So far I have this:

 public class InventoryList : MonoBehaviour {
 
     public GameObject inventoryButton;
 
     public List<Item> inventory = new List<Item>();
     ItemDatabase database;
 
     public Transform contentPanel;
 
     void Start () {
         PopulateInventory ();
     }
 
     void PopulateInventory(){
 
         database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<ItemDatabase> ();
 
         foreach (var item in inventory) {
             GameObject newInventory = Instantiate (inventoryButton) as GameObject;
             InventoryItem iButton = newInventory.GetComponent <InventoryItem> ();
             iButton.WeaponName.text = item.itemName;
             iButton.WeaponType.text = item.itemType.ToString ();
             iButton.WeaponLevel.text = item.itemLevel.ToString ();
             iButton.UpgradeCost.text = item.itemUpgradeCost.ToString ();
             iButton.ScrapCost.text = item.itemScrapCost.ToString ();
             iButton.maxIcon.SetActive (item.itemIsMaxLevel);
             iButton.Icon.sprite = item.itemIcon;
             newInventory.transform.SetParent (contentPanel, false);
         }
     }
 }

I also have an inventory database where I want to store the various items then create an inventory item when the player gets an item. The database code is:

 [System.Serializable]
 public class Item {
 
     public GameObject weapon;
     public string itemName;
     public int itemID;
     public int itemUpgradeCost;
     public int itemScrapCost;
     public int itemLevel;
     public Sprite itemIcon;
     public bool itemIsMaxLevel;
     public ItemType itemType;
     
     public enum ItemType {
         Primary, Secondary, Consumable
     }
 }
 
 public class ItemDatabase : MonoBehaviour {
 
     void Start(){
 
     }
 }

but I'm stuck on how to create the item in the database and have it feed the PopulateInventory. It works fine if I manually add it in the inspector.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cherno · Mar 11, 2015 at 10:27 PM

So you need a List that acts as your database, right? From this list, various pre-set items can be pulled and added to the player's inventory. I don't know why the item database needs it's own class if it's just a list anyway, but if you insist then just apply the following code accordingly.

 public List<Item> itemDatabase;
 
 void Start() {
      itemDatabase = new List<Item>() {
           new Item() {
                itemName = "Sword",
                itemID = 0,
                itemUpgradeCost = 23
                //... and so on
           },
           new Item() {
                itemName = "Shield",
                itemID = 1,
                itemUpgradeCost = 12
                //... and so on
           }
           //... and so on
      }
 }
 
 Even better would be a Dictionary so we can use the item ID or item name as the key and get the item as a value.

 public Dictionay<string,Item> itemDataBaseDictionary;
 
 void Start() {
      foreach(Item item in itemDatabase) {
           itemDatabaseDictionary.Add(item.itemName, item);
      }
 }


You could of course directly create the dictionary without creating a list beforehand. I suggest looking into XML, it's a very handy way of keeping things like item databases in a structured way and you don't have to create items in a script.

Also be ware that if you add an item to the inventory, you merely create a reference in that list, which references the item in the databaase list. That means that if you change the inventory item, it will also be changed in the database list, and you probably don't want that. This means that you need a function that actually creates a new item in your inventory by copying the values from the database item.

 public Item CopyItemValues(Item sourceItem, Item targetItem) {
         FieldInfo[] sourceFields = sourceItem.GetType().GetFields(BindingFlags.Public | 
                                                                   BindingFlags.NonPublic | 
                                                                   BindingFlags.Instance);
         
         
         int i = 0;
         
         for(i = 0; i < sourceFields.Length; i++) {
             var value = sourceFields[i].GetValue(sourceItem);
             sourceFields[i].SetValue(targetItem, value);
         }
         
         return targetItem;
     }


You could even use a function that takes any two Types and checks if fields that are present in the source Type are present in the target Type, and copy the values if yes. This has the advantage of not needing to pass the function specific Types (like Item). Handy if you also need to copy values from a vehicle database or whatever. This however, goes beyond the scale of this answer but there are lots of examples on the net, especially on Stack Overflow.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

*What are some ways of implementing a Mid Mission Weapons/Upgrade/Equipment store? 0 Answers

PLEASE HELP! How can I add the material component from this scriptable object to a public list? My goal is to allow the player to apply materials to game objects from that list. 0 Answers

Using enums to design an inventory system? 1 Answer

Inventory armor wielding proplem,How to convert from derived to base 1 Answer

How do I retrieve an item from a list? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges