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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Need help with inventory system, getting data from an array and using it.

Discussion in 'Scripting' started by VileGoo, Sep 14, 2018.

  1. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    I'm trying to setup an inventory system for my game, but I think I'm doing it wrong because I'm having a lot of trouble wrapping my head around it to make things work. Basically the inventory system is made up of two scripts: InventoryController.cs and InventoryUI.cs. The InventoryController is used to add a ScriptableObject assigned to an object you can interact with into an array. The InventoryUI is then supposed to get this data from the array and assign it to the proper slot so it can visually be inside of the players inventory. However I'm having a really hard to figuring out how to do this, along with getting the data from the ScriptableObject so the InventoryUI can use it. Basically, the item ScriptableObject currently has 4 properties: a string name, a sprite icon, a string description, and a string unique name index (so stuff like keys can only work with one door). I need to get this data so the inventory slot can use it, I have no idea how though. Help would be appreciated!

    Code (CSharp):
    1. public class InventoryController : MonoBehaviour
    2. {
    3.     public ScriptableObject[] InventorySlots = new ScriptableObject[8];
    4.  
    5.     InventoryUI inventoryUI;
    6.  
    7.     void Start()
    8.     {
    9.         inventoryUI = GameObject.Find("InventoryController").GetComponent<InventoryUI>();
    10.     }
    11.  
    12.     public void AddItem(ScriptableObject itemToAdd, GameObject interacted)
    13.     {
    14.         // Loop through all inventory slots and get which ones are currently empty and add the item to it
    15.         for (int i = 0; i < InventorySlots.Length; i++)
    16.         {
    17.             if (InventorySlots[i] == null)
    18.             {
    19.                 InventorySlots[i] = itemToAdd;
    20.                 //inventoryUI.AddItemToSlot(); // What do I send from here to get the slot the item should be assinged to and the ScriptableObject it needs to show?
    21.                 Destroy(interacted);
    22.                 return;
    23.             }
    24.         }
    25.         Debug.LogError("Inventory Full");
    26.     }
    27.  
    28. }

    Code (CSharp):
    1. public class InventoryUI : MonoBehaviour
    2. {
    3.     public int slots;
    4.     public GameObject slotPrefab;
    5.     public Transform inventorySlotParent;
    6.  
    7.     public GameObject inventoryUI;
    8.  
    9.     int numIteration = 0;
    10.  
    11.     void Start()
    12.     {
    13.         for (int i = 0; i < slots; i++)
    14.         {
    15.             slotPrefab = Instantiate(slotPrefab, transform.parent.position, transform.parent.rotation, inventorySlotParent);
    16.             slotPrefab.name = ("_Slot" + numIteration); // Assign a unique name to each slot
    17.             numIteration++;
    18.         }
    19.     }
    20.  
    21.     public void AddItemToSlot()
    22.     {
    23.         // Add the data recieved from InteractionController to the correct slot
    24.     }
    25. }

    Code (CSharp):
    1. [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Generic Item")]
    2. public class Item_Generic : ScriptableObject
    3. {
    4.     new public string name = "New Item";
    5.     public Sprite icon = null;
    6.     [TextArea]
    7.     public string description = "New item description";
    8.     [Tooltip("Must be a unique name. Other scripts will check for this property to determine if it's the correct one, not the name.")]
    9.     public string index = "unique_index";
    10. }
     
  2. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    You need to establish references between the various components so they can communicate. What i did for my inventory was something like this:

    The Inventory script had a reference to a ui object. The ui object had all of the inventory slots setup by me in the editor, and i would link this ui parent object to the inventory in the inspector. On start, the inventory would use GetComponentsInChildren to grab all of the InventorySlot references on the ui object and its children.

    Now that the inventory has a reference to all of its available slots, you need to let the slots know about the inventory. Loop through all the slots you just got in the inventory start function, and setup each slot to have a reference back to the inventory.

    So now your inventory has access to all of the slot components, and all of the slot components can talk back to the inventory.

    All thats left now is to give the slot components a reference to a scriptable object item. When an item gets added to a slot, you set this reference to the item it contains. So now your slot script can access the item.

    Hopefully that helps. good luck!
     
    VileGoo likes this.
  3. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    Yeah i'm still lost, i'm having no luck getting anywhere with assigning the UI slots to each element in the array.
     
  4. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    Bump. I still have no idea what to do. :mad:
     
  5. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    Bump.
     
  6. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    sorry... exactly... what do you need?
     
  7. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    I need it so when an item is added to the InventorySlot array inside InventoryController.cs it will send that information about what slot and what item (sciptable object) was added to the InventoryUI.cs script so the inventory slot UI updates and shows the item in the correct slot with the information inside the scriptable object that was added to the array element from InventoryController.cs. I'm having a lot of trouble figuring out how to do this.
     
  8. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    if you add your ítem to scene you can detect when the player enter in collission with it then you can Access to component of this ítem and send its data to do a foreach to roam you array of slots and return the first empty slot then you can set the atributes of you slot(Im assuming that you slot have a attribute called "Empty" and hmmm… "storedObject=(?) in this momento you can call you method to update you UI.(Sorry for my abd english c: )
    c:
     
  9. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    Bump.
     
  10. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Try passing the ScriptableObject as a parameter, along with the index.

    Code (CSharp):
    1. public class InventoryController : MonoBehaviour
    2. {
    3.     public ScriptableObject[] InventorySlots = new ScriptableObject[8];
    4.     InventoryUI inventoryUI;
    5.     void Start()
    6.     {
    7.         inventoryUI = GameObject.Find("InventoryController").GetComponent<InventoryUI>();
    8.     }
    9.     public void AddItem(ScriptableObject itemToAdd, GameObject interacted)
    10.     {
    11.         // Loop through all inventory slots and get which ones are currently empty and add the item to it
    12.         for (int i = 0; i < InventorySlots.Length; i++)
    13.         {
    14.             if (InventorySlots[i] == null)
    15.             {
    16.                 InventorySlots[i] = itemToAdd;
    17.                 inventoryUI.AddItemToSlot(i, itemToAdd); // What do I send from here to get the slot the item should be assinged to and the ScriptableObject it needs to show?
    18.                 Destroy(interacted);
    19.                 return;
    20.             }
    21.         }
    22.         Debug.LogError("Inventory Full");
    23.     }
    24. }

    Code (CSharp):
    1. public class InventoryUI : MonoBehaviour
    2. {
    3.     public int slots;
    4.     public GameObject slotPrefab;
    5.     public Transform inventorySlotParent;
    6.     public GameObject inventoryUI;
    7.     int numIteration = 0;
    8.     void Start()
    9.     {
    10.         for (int i = 0; i < slots; i++)
    11.         {
    12.             slotPrefab = Instantiate(slotPrefab, transform.parent.position, transform.parent.rotation, inventorySlotParent);
    13.             slotPrefab.name = ("_Slot" + numIteration); // Assign a unique name to each slot
    14.             numIteration++;
    15.         }
    16.     }
    17.     public void AddItemToSlot(int slotIndex, ScriptableObject itemToAdd)
    18.     {
    19.         // Add the data recieved from InteractionController to the correct slot
    20.     }
    21. }
     
  11. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    whats is the meaning of Bump?
     
  12. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    When a comment is added it places the thread at the top of the forum. I'd rather bump it instead of posting multiple threads.
     
  13. Riukensay

    Riukensay

    Joined:
    Jan 16, 2018
    Posts:
    49
    but you got a lot of answers… why you do that?
     
    bobisgod234 likes this.