Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple Inventory System Using Scriptable Objects

Discussion in 'Projects In Progress' started by Bl00dyFish, Oct 15, 2023.

  1. Bl00dyFish

    Bl00dyFish

    Joined:
    Mar 12, 2022
    Posts:
    70
    Hey all! I just wanted to show off the inventory system that I've been working on for the past week now. I would really like it if you could give me any feedback, or suggest any feature that would make it even better. But anyways, here's how it works.

    First, I created a Scriptable Item Item:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. [CreateAssetMenu]
    7. public class Item : ScriptableObject
    8. {
    9.     public string ItemName;
    10.     public enum ItemRarity {Common, Epic, Legendary, GodTier};
    11.     public ItemRarity itemRarity;
    12.     public float Damage;
    13.     public float Range;
    14.     public float NumberOfBulletsAbleToBeHeld;
    15.     public Sprite ItemIcon;
    16.     public GameObject gameObject;
    17.    
    18. }

    Then I created another script referencing this. (To put on other game objects)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PhysicalItem : MonoBehaviour
    7. {
    8.     public Item item;
    9.  
    10. }
    11.  
    I attached an image of my Canvas hierarchy(which is important because the code for organizing the inventory kind of relies on knowing where the children of each slot reside and their order on the hierarchy).

    I also created a prefab Slot, which consists of an image and text box (which will be for displaying ammo when I get around to implementing it). This prefab will be instantiated on the panel element that the player has selected and the image will change based on the item the player picks up. I also attached the PhysicalItem script to the slot prefab so we can always know which gun belongs to which slot.

    Anyways, the code for the inventory is below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using UnityEngine.UI;
    6. public class Inventory : MonoBehaviour
    7. {
    8.     public float numberOfSlotSelected = 1;
    9.     public Transform selectedPanel;
    10.     Transform availablePanel;
    11.     public GameObject slot;
    12.     public List<Item> inventory;
    13.     public Image select;
    14.     public TMP_Text selectedGunText;
    15.     public List<Transform> availableSlots;
    16.     public List<Transform> possibleSlots;
    17.     public List<Transform> closestAvailablePanel;
    18.    
    19.    
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.  
    24.         numberOfSlotSelected = 1;
    25.         inventory = new List<Item>();
    26.         availableSlots = new List<Transform>();
    27.         closestAvailablePanel = new List<Transform>();
    28.  
    29.        int i = transform.childCount;
    30.      
    31.         while(i > 0)
    32.         {
    33.             if(transform.GetChild(i - 1).childCount == 0)
    34.             {
    35.                 possibleSlots.Add(transform.GetChild(i - 1)); //becuase child index starts at 0
    36.             }
    37.             i--;
    38.          
    39.         }
    40.     }
    41.  
    42.     // Update is called once per frame
    43.     void Update()
    44.     {
    45.      
    46.      
    47.  
    48.         int numberOfSlotSelected_Int = (int)numberOfSlotSelected;
    49.         numberOfSlotSelected += Input.mouseScrollDelta.y;
    50.        
    51.        
    52.         selectedPanel = transform.GetChild(numberOfSlotSelected_Int - 1); //We use -1 because unity starts egtting children at 0. So the first child would be child 0 not child 1
    53.        
    54.  
    55.         if (numberOfSlotSelected > 5)
    56.         {
    57.             numberOfSlotSelected = 1;
    58.         }
    59.      
    60.         if (numberOfSlotSelected < 1)
    61.         {
    62.             numberOfSlotSelected = 5;
    63.         }
    64.  
    65. //=======================================================================================================================================================================================//
    66.         //DETERMINE WHICH PANEL IS OPEN
    67.  
    68.  
    69.         closestAvailablePanel.Clear();
    70.             foreach (Transform available in availableSlots)
    71.             {
    72.  
    73.                 if (Vector3.Distance(selectedPanel.position, available.position) <= 100 && available.childCount == 0)
    74.                 {
    75.                     closestAvailablePanel.Add(available);
    76.  
    77.                 }
    78.  
    79.             }
    80.  
    81.             if (selectedPanel.childCount == 0)
    82.             {
    83.                 availablePanel = selectedPanel;
    84.             }
    85.  
    86.             if (selectedPanel.childCount >= 1)
    87.             {
    88.                 if (closestAvailablePanel.Count >= 1)
    89.                 {
    90.                     availablePanel = closestAvailablePanel[0];
    91.  
    92.                 }
    93.                
    94.                 //THIS IS BASICALLY FOR IF YOU KEEP YOUR CURSOR ON THE FIRST SLOT THE WHOLE TIME WHILE PICKING UP ITEMS
    95.                 if(availableSlots.Count > 0 && selectedPanel != possibleSlots[0])
    96.                 {
    97.                     if (transform.GetChild(numberOfSlotSelected_Int).childCount >= 1)
    98.                     {
    99.                         closestAvailablePanel.Clear();
    100.                         foreach (Transform available in availableSlots)
    101.                         {
    102.                             availablePanel = availableSlots[availableSlots.Count - 1];
    103.                         }
    104.                     }
    105.                 }
    106.                 //THIS IS BASICALLY FOR IF YOU KEEP YOUR CURSOR ON THE LAST SLOT THE WHOLE TIME WHILE PICKING UP ITEMS
    107.                 if(availableSlots.Count > 0 && selectedPanel == possibleSlots[0])
    108.                 {
    109.                     if (transform.GetChild(numberOfSlotSelected_Int - 2).childCount >= 1)
    110.                     {
    111.                         closestAvailablePanel.Clear();
    112.                         foreach (Transform available in availableSlots)
    113.                         {
    114.                             availablePanel = availableSlots[availableSlots.Count - 1];
    115.                         }
    116.                     }
    117.                 }
    118.  
    119.  
    120.             }
    121.  
    122.             //THIS IS FOR WHEN THERE IS ONLY ONE SLOT LEFT
    123.             if (availableSlots.Count == 1)
    124.             {
    125.                 availablePanel = availableSlots[0];
    126.             }
    127.  
    128. //=======================================================================================================================================================================================//      
    129.        
    130.         select.transform.position = selectedPanel.position;
    131.        
    132.  
    133.         //SHOW SELECTED ITEM'S NAME
    134.         if (selectedPanel.childCount >= 1)
    135.         {
    136.             selectedGunText.text = selectedPanel.GetComponentInChildren<PhysicalItem>().item.ItemName;
    137.         }
    138.         else if(selectedPanel.childCount == 0)
    139.         {
    140.             selectedGunText.text = "";
    141.         }
    142.        
    143.        
    144.     }
    145.  
    146.    
    147.     public void Add(Item pickedUpItem)
    148.     {
    149.        
    150.         GameObject instantiatedSlot = Instantiate(slot, availablePanel.position, Quaternion.identity);
    151.  
    152.         instantiatedSlot.transform.SetParent(availablePanel);
    153.         instantiatedSlot.transform.GetChild(0).GetComponent<TMP_Text>();
    154.         instantiatedSlot.transform.GetChild(1).GetComponent<Image>().sprite = pickedUpItem.ItemIcon;
    155.         instantiatedSlot.GetComponent<PhysicalItem>().item = pickedUpItem;
    156.  
    157.  
    158.         availableSlots.Clear();
    159.         foreach (Transform available in possibleSlots)
    160.         {
    161.             if (available.childCount == 0)
    162.             {
    163.                 availableSlots.Add(available);
    164.  
    165.             }
    166.         }
    167.        
    168.  
    169.     }
    170.  
    171. }
    And the code for picking up the game objects is in my player controller script:

    Code (CSharp):
    1.     void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.CompareTag("Item"))
    4.         {
    5.             if(GameObject.Find("Inventory").GetComponent<Inventory>().inventory.Count < 5)
    6.             {
    7.                  GameObject.Find("Inventory").GetComponent<Inventory>().inventory.Add(other.gameObject.GetComponent<PhysicalItem>().item);
    8.                  GameObject.Find("Inventory").GetComponent<Inventory>().Add(other.gameObject.GetComponent<PhysicalItem>().item);
    9.            
    10.                  Destroy(other.gameObject);
    11.             }
    12.  
    13.  
    14.         }
    15.     }
    Again, I just made this in a week, and currently, I have no way to drop items, but I'm still developing it and wil post updates here if anyone is interested.

    Again, I would really appreciate any feedback you might have.

    Thanks!
     

    Attached Files:

  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,496
    Your item description is moving in the right way, but:

    * You don't have any sort of "description".
    * If you plan to localize the game at some point, things might get interesting.
    * You do not denote item type anywhere. Ammo, weapons and armor will be processed quite differently in any game.
    * Rarity probably can be just denoted by a number instead of enum. So when you decide to add one more tier later you wouldn't need to go through all your code.
    * Probably the best to store item counts as ints. Same likely applies to damage as well.

    Regarding your inventory class...

    I'm not feeling it.

    Inventory stored on an object should be a simple array, list or grid with no visual representation. And you'll only need grid if you use grid-based system. You start spawning visible "slots" in UI only when you DISPLAY the inventory. In your case, however, an object stores slots as transforms and inventory as a monobehavior. Is it necessary? Naked C# classes would suffice here.

    Consider this. A dead enemy, a barrel, a chest, or a storage container will all have their inventories. Do you plan to attach gameobject slots to all of them? What if there's a hundred containers nearby?

    Also read about Model View Controller architecture.
     
    angrypenguin likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,332
    I mean looking at it, it's a simple inventory system. Does it fulfil your requirements? Or does your game require more?

    Mind you, I would still definitely have a stronger separation between the data and visuals here. The "Inventory" part of an inventory should just be the part that handles the storage of items; no involvement with visuals or anything else.

    Then you just need to provide some hooks that allows any visuals to respond to changed (basically delegates). It becomes a lot easier to modify things without worrying about how other parts of it work.

    So pretty much what neginfinity said above. With this kind of stuff, you'll need to get very used to operating in the pure C# work.

    I would denote rarity with a scriptable object, myself. That way, any information about said rarity (text colour, etc), can all be baked into said scriptable object. And adding more rarities is trivial.
     
  4. Bl00dyFish

    Bl00dyFish

    Joined:
    Mar 12, 2022
    Posts:
    70
    What do you mean when you say stronger separation between data and visuals? Wouldn't an inventory mostly be visual? Then again this is my first successful attempt ever making an inventory system.
     
  5. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    528
    A game can run with no visuals and no player controllers, just models and AI controllers in a black box. That's how some AI trains, by just playing the game against itself over and over. Separating the modeled state of your game from the controllers that stimulate those models and from the view through which the state of the game is rendered can help to keep each of those aspects modular.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,332
    No, the visual part of an inventory should only represent the underlying data. An item's definition is data. An collection of items is just data. But the UI that displays these items and their information is the visuals. Have a clear distinction between the two and it makes things a lot more modular.

    Right now your 'Inventory' component smashes both data and visuals together, and will become harder to work with as you add in more features.
     
  7. Bl00dyFish

    Bl00dyFish

    Joined:
    Mar 12, 2022
    Posts:
    70
    Thank you! What in your opinion would be the best place to start in making the system less focused on the visuals? What do you suggest I focus on?
     
  8. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,636
    A general aproach is to have something like a StateManager. A "single source of truth". That means all abstracted information about the current inventory is stored by members of that unique instance.
    The visualisation is then built separately and only queries from that StateManager. Its members can only be accessed via ReadOnlyList and similar. To modify the state, explicit methods on the StateManager need to called by the UI.

    This aproach makes serialization of the inventory very easy. The tricky part is to synchronize this state with the UI at all times.

    One aproach that helps with the later issue and one Unity uses internally for its canvas system is to give the content a variable like "dirty". Content in your case is the StateManager.
    Whenever the state is changed via the mentioned methods, it is marked as dirty.

    Then there's an update loop which checks if the state was marked as dirty and then rebuilds the whole inventory before setting dirty to false again.
    Only having to do rebuilds instead of shuffling around existing visual elements can make that step far easier to implement and secure against edgecases. However it's also more performance intense (hence why the Unity canvas system sometimes is occasionally said to be a performance problem; yet careless usage (e.g. fast updating elements on same canvas as everything else) is often an avoidable cause).
    For most games you may not need to worry about that and if it becomes an issue you can find a hybrid aproach via object pooling...
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,332
    It's not about 'less focus on the visuals', it's about separation. About having a distinction between the data side and visuals side. Both sides are still important, of course.

    An inventory object, for example, should just be a kind of 'collection' object; ergo, it's an object that represents a collection of items. It can have methods for adding/removing items (or perhaps, methods to attempt to do so), delegates for when the inventory changes, and properties and methods to find out information about it and about the items inside of it. Though nothing inside the object has anything to do with UI or other in-game visuals.

    Then separate to that, is the actual UI to display what's in an inventory. It can take an inventory object, and use it's API to display its contents. Any modification of the inventory is handled through the UI, which effectively just calls the inventory object's API.

    In ultra basic code terms:
    Code (CSharp):
    1. // item asset that outlines information about an item
    2. public class ItemObject : ScriptableObject { }
    3.  
    4. // pure C# object to represent a collection of items
    5. [System.Serializable]
    6. public class ItemCollection : IEnumerable<ItemObject>
    7. {
    8.     #region Inspector Fields
    9.    
    10.     [SerializeField]
    11.     private List<ItemObject> _inventoryObjects = new();
    12.    
    13.     #endregion
    14.    
    15.     #region Properties
    16.    
    17.     public int Count => _inventoryObjects.Count;  
    18.    
    19.     public Action<ItemObject> OnItemAdded;
    20.    
    21.     public Action<ItemObject> OnItemRemoved;
    22.    
    23.     #endregion
    24.    
    25.     #region Inventory Methods
    26.    
    27.     public ItemObject GetItem(int index)
    28.     {
    29.         return _inventoryObjects[index];
    30.     }
    31.    
    32.     public bool ContainsItem(ItemObject item)
    33.     {
    34.         return _inventoryObjects.Contains(item);
    35.     }
    36.    
    37.     public bool TryAddItem(ItemObject item)
    38.     {
    39.         bool hasItem = this.ContainsItem(item);
    40.        
    41.         if (hasItem == false)
    42.         {
    43.             _inventoryObjects.Add(item);
    44.             OnItemAdded?.Invoke(item);
    45.         }
    46.        
    47.         return !hasItem;
    48.     }
    49.    
    50.     public bool TryRemoveItem(ItemObject item)
    51.     {
    52.         bool hasItem = this.ContainsItem(item);
    53.        
    54.         if (hasItem == true)
    55.         {
    56.             _inventoryObjects.Remove(item);
    57.             OnItemRemoved.Invoke(item);
    58.         }
    59.        
    60.         return hasItem;
    61.     }
    62.    
    63.     #endregion
    64.    
    65.     #region Interface Methods
    66.    
    67.     public IEnumerator<ItemObject> GetEnumerator()
    68.     {
    69.         return _inventoryObjects.GetEnumerator();
    70.     }
    71.    
    72.     IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    73.    
    74.     #endregion
    75. }
    76.  
    77. // can wrap the item collection in a scriptable object
    78. // for editing in the inspector
    79. public class ItemCollectionObject : ScriptableObject
    80. {
    81.     [SerializeField]
    82.     private ItemCollection _itemCollection = new();
    83.    
    84.     public ItemCollection ItemCollection => _itemCollection;
    85. }
    86.  
    87. // then a completey separate monobehaviour
    88. // for handling the user interface
    89. public class InventoryUserInterface : Monobehaviour
    90. {
    91.     public void OpenInventory(ItemCollection itemCollection)
    92.     {
    93.         // UI stuff
    94.     }
    95. }
    Not going to go into detail about the actual UI code stuff, but you can see how there's a proper divide here. This could be abstracted further with interfaces, perhaps, if you require a variety of different kinds of container objects.

    The UI stuff will of course require a number of different components the handle various parts of the UI, or visual elements if you're using UI Toolkit.

    All in all this starts to get pretty complicated pretty quickly, depending on your requirements.
     
    Bl00dyFish and CodeRonnie like this.
  10. Bl00dyFish

    Bl00dyFish

    Joined:
    Mar 12, 2022
    Posts:
    70
    Thank you so much!
     
    CodeRonnie likes this.
  11. Bl00dyFish

    Bl00dyFish

    Joined:
    Mar 12, 2022
    Posts:
    70
    Okay! Update:
    **Note that this is what is needed for my game, and isn't for super complex inventories**
    I created a separate script to manage what is inside the inventory and put it on the Player:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.     public List<Item> inventory;
    8.     public static Inventory instance;
    9.  
    10.  
    11.      void Awake()
    12.     {
    13.         instance = this;
    14.     }
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         inventory = new List<Item>();
    20.     }
    21.  
    22. }
    23.  
    Then I created a separate script called InventoryUiController, where I placed most of the previous code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class InventoryUiController : MonoBehaviour
    8. {
    9.     public float numberOfSlotSelected = 1;
    10.  
    11.     public Transform selectedPanel;
    12.     Transform availablePanel;
    13.  
    14.     public GameObject slot;
    15.     public Transform gunSpawnPoint;
    16.     GameObject heldGun;
    17.  
    18.  
    19.     public Image select;
    20.     public TMP_Text selectedGunText;
    21.  
    22.     public Item selectedItem;
    23.  
    24.     public List<Transform> availableSlots;
    25.     public List<Transform> possibleSlots;
    26.     public List<Transform> closestAvailablePanel;
    27.  
    28.  
    29.  
    30.     // Start is called before the first frame update
    31.     void Start()
    32.     {
    33.  
    34.      
    35.         numberOfSlotSelected = 1;
    36.         availableSlots = new List<Transform>();
    37.         closestAvailablePanel = new List<Transform>();
    38.  
    39.         int i = transform.childCount;
    40.  
    41.         while (i > 0)
    42.         {
    43.             if (transform.GetChild(i - 1).childCount == 0)
    44.             {
    45.                 possibleSlots.Add(transform.GetChild(i - 1)); //becuase child index starts at 0
    46.             }
    47.             i--;
    48.  
    49.         }
    50.  
    51.     }
    52.  
    53.     // Update is called once per frame
    54.     void Update()
    55.     {
    56.  
    57.  
    58.  
    59.         int numberOfSlotSelected_Int = (int)numberOfSlotSelected;
    60.         numberOfSlotSelected += Input.mouseScrollDelta.y;
    61.  
    62.  
    63.         selectedPanel = transform.GetChild(numberOfSlotSelected_Int - 1); //We use -1 because unity starts egtting children at 0. So the first child would be child 0 not child 1
    64.  
    65.  
    66.         if (numberOfSlotSelected > 5)
    67.         {
    68.             numberOfSlotSelected = 1;
    69.         }
    70.  
    71.         if (numberOfSlotSelected < 1)
    72.         {
    73.             numberOfSlotSelected = 5;
    74.         }
    75.  
    76.         //=======================================================================================================================================================================================//
    77.         //DETERMINE WHICH PANEL IS OPEN
    78.  
    79.  
    80.         closestAvailablePanel.Clear();
    81.         foreach (Transform available in availableSlots)
    82.         {
    83.  
    84.             if (Vector3.Distance(selectedPanel.position, available.position) <= 100 && available.childCount == 0)
    85.             {
    86.                 closestAvailablePanel.Add(available);
    87.  
    88.             }
    89.  
    90.         }
    91.  
    92.         if (selectedPanel.childCount == 0)
    93.         {
    94.             availablePanel = selectedPanel;
    95.         }
    96.  
    97.         if (selectedPanel.childCount >= 1)
    98.         {
    99.             if (closestAvailablePanel.Count >= 1)
    100.             {
    101.                 availablePanel = closestAvailablePanel[0];
    102.  
    103.             }
    104.  
    105.             //THIS IS BASICALLY FOR IF YOU KEEP YOUR CURSOR ON THE FIRST SLOT THE WHOLE TIME WHILE PICKING UP ITEMS
    106.             if (availableSlots.Count > 0 && selectedPanel != possibleSlots[0])
    107.             {
    108.                 if (transform.GetChild(numberOfSlotSelected_Int).childCount >= 1)
    109.                 {
    110.                     closestAvailablePanel.Clear();
    111.                     foreach (Transform available in availableSlots)
    112.                     {
    113.                         availablePanel = availableSlots[availableSlots.Count - 1];
    114.                     }
    115.                 }
    116.             }
    117.             //THIS IS BASICALLY FOR IF YOU KEEP YOUR CURSOR ON THE LAST SLOT THE WHOLE TIME WHILE PICKING UP ITEMS
    118.             if (availableSlots.Count > 0 && selectedPanel == possibleSlots[0])
    119.             {
    120.                 if (transform.GetChild(numberOfSlotSelected_Int - 2).childCount >= 1)
    121.                 {
    122.                     closestAvailablePanel.Clear();
    123.                     foreach (Transform available in availableSlots)
    124.                     {
    125.                         availablePanel = availableSlots[availableSlots.Count - 1];
    126.                     }
    127.                 }
    128.             }
    129.  
    130.  
    131.         }
    132.  
    133.         //THIS IS FOR WHEN THERE IS ONLY ONE SLOT LEFT
    134.         if (availableSlots.Count == 1)
    135.         {
    136.             availablePanel = availableSlots[0];
    137.         }
    138.  
    139.         //=======================================================================================================================================================================================//    
    140.  
    141.         select.transform.position = selectedPanel.position;
    142.  
    143.  
    144.         //SELECTED ITEM
    145.         if (selectedPanel.childCount >= 1)
    146.         {
    147.             selectedItem = selectedPanel.GetComponentInChildren<PhysicalItem>().item;
    148.         }
    149.  
    150.         //SHOW SELECTED ITEM'S NAME
    151.         if (selectedPanel.childCount >= 1)
    152.         {
    153.             selectedGunText.text = selectedItem.ItemName;
    154.         }
    155.         else if (selectedPanel.childCount == 0)
    156.         {
    157.             selectedGunText.text = "";
    158.         }
    159.  
    160.  
    161.  
    162.  
    163.  
    164.         //HOLD GUN  
    165.         if (selectedPanel.childCount >= 1)
    166.         {
    167.  
    168.             if (gunSpawnPoint.childCount == 0)
    169.             {
    170.                 heldGun = Instantiate(selectedItem.itemGameObject, gunSpawnPoint.position, gunSpawnPoint.rotation);
    171.                 heldGun.transform.SetParent(gunSpawnPoint);
    172.  
    173.             }
    174.  
    175.             if (gunSpawnPoint.childCount >= 1)
    176.             {
    177.  
    178.                 if (heldGun.GetComponent<PhysicalItem>().item.ItemName == selectedItem.name)
    179.                 {
    180.                     print("same gun");
    181.                 }
    182.                 else if (heldGun.GetComponent<PhysicalItem>().item.ItemName != selectedItem.name)
    183.                 {
    184.                     print("different gun");
    185.                     Destroy(heldGun);
    186.                     heldGun = Instantiate(selectedItem.itemGameObject, gunSpawnPoint.position, gunSpawnPoint.rotation);
    187.                     heldGun.transform.SetParent(gunSpawnPoint);
    188.                 }
    189.  
    190.             }
    191.  
    192.         }
    193.         if (selectedPanel.childCount == 0)
    194.         {
    195.             Destroy(heldGun);
    196.         }
    197.  
    198.  
    199.     }
    200.  
    201.  
    202.     public void Add(Item pickedUpItem)
    203.     {
    204.  
    205.         GameObject instantiatedSlot = Instantiate(slot, availablePanel.position, Quaternion.identity);
    206.  
    207.         instantiatedSlot.transform.SetParent(availablePanel);
    208.         instantiatedSlot.transform.GetChild(0).GetComponent<TMP_Text>();
    209.         instantiatedSlot.transform.GetChild(1).GetComponent<Image>().sprite = pickedUpItem.ItemIcon;
    210.  
    211.         instantiatedSlot.GetComponent<PhysicalItem>().item = pickedUpItem;
    212.  
    213.  
    214.         availableSlots.Clear();
    215.         foreach (Transform available in possibleSlots)
    216.         {
    217.             if (available.childCount == 0)
    218.             {
    219.                 availableSlots.Add(available);
    220.  
    221.             }
    222.         }
    223.  
    224.  
    225.     }
    226.  
    227. }
    228.  
    I also updated the method by which you are able to pick up items. Instead of walking straight into a gun to pick it up, you have to look at it and press 'E'. The code is also pretty much the same. (I placed the portion of code in a separate script meant to handle raycasts and shooting):

    Code (CSharp):
    1.     void PickUp()
    2.     {
    3.         if (GetComponent<Inventory>().inventory.Count < 5)
    4.         {
    5.             GetComponent<Inventory>().inventory.Add(selectedObject.GetComponent<PhysicalItem>().item); //ADD TO INVENTORY
    6.             GameObject.Find("Inventory").GetComponent<InventoryUiController>().Add(selectedObject.gameObject.GetComponent<PhysicalItem>().item);
    7.  
    8.             Destroy(selectedObject);
    9.         }
    10.     }
     
    Last edited: Oct 25, 2023