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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Hi I need some help. Need Help WIth Inventory System and Using Items

Discussion in '2D' started by kdawgfsho04, Nov 13, 2020.

  1. kdawgfsho04

    kdawgfsho04

    Joined:
    Aug 3, 2020
    Posts:
    18
    Hi Im very much still a Newby at this but I made an Inventory System and i would like to use some of the items to give me health if i click on it in my inventory.

    here is my Inventory, the Item and not sure if you need it but thebuttonItem i use for removing the items from inventory.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class gameManager : MonoBehaviour
    8. {
    9.     public static gameManager instance;
    10.     public bool isPaused;
    11.     public List<Item> items = new List<Item>();// WHAT KIND OF ITEMS
    12.     public List<int> itemNumbers = new List<int>();//HOW MANY WE HAVE
    13.     public GameObject[] slots;
    14.     //public.Dictionary<Item, int> itemDict = new Dictionary<Item, int>();//OPTIONAL
    15.     //public Item addItem_01;//TO DO REMOVE LATER
    16.     //public Item removeItem_01;//TO DO REMOVE LATER
    17.     public ItemButton thisButton;//KEEP TRACK OF WHICH ITEM WE ARE HOVERING OVER
    18.     public ItemButton[] itemButtons;//ALL THE ITEM BUTTONS IN GAME[USED FOR RESET]
    19.  
    20.  
    21.     private void Awake()
    22.     {
    23.         if (instance == null)
    24.         {
    25.             instance = this;
    26.         }
    27.         else
    28.         {
    29.             if (instance != this)
    30.             {
    31.                 Destroy(gameObject);
    32.             }
    33.         }DontDestroyOnLoad(gameObject);
    34.     }
    35.  
    36.     public void Start()
    37.     {
    38.         DisplayItems();
    39.      
    40.     }
    41.     private void DisplayItems()
    42.     {
    43.         #region
    44.         /*for (int i = 0; i < items.Count; i++)
    45.         {
    46.             //UPDATE SLOTS ITEM IMAGE
    47.             slots.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 1f);
    48.             slots.transform.GetChild(0).GetComponent<Image>().sprite = items.itemSprite;
    49.  
    50.             //UPDATE SLOTS COUNT TEXT
    51.             slots.transform.GetChild(1).GetComponent<Text>().color = new Color(1f, 1f, 1f, 1f);
    52.             slots.transform.GetChild(1).GetComponent<Text>().text = itemNumbers.ToString();
    53.  
    54.             //UPDATE CLOSE/THROW BUTTON
    55.             slots.transform.GetChild(2).gameObject.SetActive(true);
    56.         }*/
    57.         #endregion
    58.  
    59.         //WE IGNORE THE FACT
    60.         for (int i = 0; i < slots.Length; i++)
    61.         {
    62.             if (i < items.Count)
    63.             {
    64.                 //UPDATE SLOTS ITEM IMAGE
    65.                 slots.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 1f);
    66.                 slots.transform.GetChild(0).GetComponent<Image>().sprite = items.itemSprite;
    67.  
    68.                 //UPDATE SLOTS COUNT TEXT
    69.                 slots.transform.GetChild(1).GetComponent<Text>().color = new Color(1f, 1f, 1f, 1f);
    70.                 slots.transform.GetChild(1).GetComponent<Text>().text = itemNumbers.ToString();
    71.  
    72.                 //UPDATE CLOSE/THROW BUTTON
    73.                 slots.transform.GetChild(2).gameObject.SetActive(true);
    74.             }
    75.             else//SOME REMOVE ITEMS
    76.             {
    77.                 //UPDATE SLOTS ITEM IMAGE
    78.                 slots.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 0f);
    79.                 slots.transform.GetChild(0).GetComponent<Image>().sprite = null;
    80.  
    81.                 //UPDATE SLOTS COUNT TEXT
    82.                 slots.transform.GetChild(1).GetComponent<Text>().color = new Color(1f, 1f, 1f, 0f);
    83.                 slots.transform.GetChild(1).GetComponent<Text>().text = null;
    84.  
    85.                 //UPDATE CLOSE/THROW BUTTON
    86.                 slots.transform.GetChild(2).gameObject.SetActive(false);
    87.             }
    88.         }
    89.     }
    90.     private void Update()
    91.     {/*
    92.         if (Input.GetKeyDown(KeyCode.M))
    93.         {
    94.             AddItem(addItem_01);
    95.         }
    96.         if (Input.GetKeyDown(KeyCode.N))
    97.         {
    98.             RemoveItem(removeItem_01);
    99.         }*/
    100.     }
    101.      public void AddItem(Item _item)
    102.      {
    103.         //MARKER IF THERE IS ONE EXISTING ITEM IN OUR BAG/LIST
    104.         if (!items.Contains(_item))
    105.         {
    106.             items.Add(_item);
    107.             itemNumbers.Add(1);//ADD ONE
    108.         }
    109.         //IF THERE IS A NEW _ITEM IN OUR BAG
    110.         else
    111.         {
    112.             UnityEngine.Debug.Log("You Already Have This One.");
    113.             for (int i = 0; i < items.Count; i++)
    114.             {
    115.                 if (_item == items)
    116.                 {
    117.                     itemNumbers[i]++;
    118.                 }
    119.             }
    120.         }
    121.         DisplayItems();
    122.      }
    123.      public void RemoveItem(Item _item)
    124.     {
    125.         //IF ONE IS EXISTING IN OUR INV
    126.      
    127.         if (items.Contains(_item))
    128.         {
    129.             for (int i = 0; i < items.Count; i++)
    130.             {
    131.                 if (_item == items[i])
    132.                 {
    133.                  
    134.                     itemNumbers[i]--;
    135.                     if (itemNumbers[i] == 0)
    136.                     {
    137.                         //WE HAVE TO REMOVE THIS ITEM
    138.                         items.Remove(_item);
    139.                         itemNumbers.Remove(itemNumbers[i]);
    140.                      
    141.                     }
    142.                 }
    143.              
    144.             }
    145.         }
    146.         else
    147.         {
    148.             UnityEngine.Debug.Log("THERE IS NO" + _item + "IN YOUR INVENTORY");
    149.         }
    150.         //IF THERE IS NO OTHER ITEN IN INV
    151.         ResetButtonItems();
    152.         DisplayItems();
    153.     }
    154.     public void ResetButtonItems()
    155.     {
    156.         for (int i = 0; i < itemButtons.Length; i++)//FOR LOOP OF ALL BUTTONS TOTAL IS 14
    157.         {
    158.             if (i < items.Count)
    159.             {
    160.                 itemButtons[i].thisItem = items[i];
    161.             }
    162.             else
    163.             {
    164.                 itemButtons[i].thisItem = null;
    165.             }
    166.         }
    167.     }
    168.     public void Use()
    169.     {
    170.         //Item.Use();
    171.     }
    172.  
    173.  
    174. }
    175.  
    176.  
    177.  
    178. using System.Collections;
    179. using System.Collections.Generic;
    180. using UnityEngine;
    181.  
    182.  
    183. [CreateAssetMenu(menuName = "Item", fileName = "New Item")]
    184. public class Item : ScriptableObject
    185. {
    186.     public string itemName;
    187.     public string itemDes;
    188.  
    189.  
    190.     public Sprite itemSprite;
    191.     public int itemPrice;
    192.  
    193.  
    194.  
    195.  
    196.     public virtual void Use()
    197.     {
    198.        
    199.     }
    200.  
     
    Last edited: Nov 13, 2020
  2. kdawgfsho04

    kdawgfsho04

    Joined:
    Aug 3, 2020
    Posts:
    18
    i have different item types if there was a way i could click on a button and it would determine what kind of item was in the slot and apply the effects to player or whatever.. if that makes sense
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  4. lubba64_unity

    lubba64_unity

    Joined:
    Sep 16, 2020
    Posts:
    9
    hey I've been working on a game for half a year, and have a decent inventory system that works well, however explaining it on the forms would be pretty slow (because its complicated). discord maybe? lubba64#5426. if not then I would recommend this YouTube tutorial: https://www.youtube.com/watch?v=2WnAOV7nHW0&ab_channel=CodeMonkey and one last thing, inventories are really hard to do well, so be careful and make sure you actually need one in your game before you attempt to make one.

    the reason I'm not addressing your problem directly is because it looks like your code for the base inventory class could be improved significantly, because it looks like your slot, UI logic, and item distribution and storage logic are all in the same script (which can get messy and hard to maintain and update.)
    if you want to make a health potion a potion system would be in order, which would also be hard to explain. I would recommend making a robust inventory first, then adding "potion" logic. I also have a potion system of my own but its unique to my project and system, so again if you wanna see my implementation contact me on discord Lubba64#5426

    if you want to make potions make sure they can be "tagged" as a potion, give them some sort of identifier for what they should do (whether this be in a script or in your item storage solution) and make functions that you can assign to these identifiers so it can execute that function when the action for drinking a potion occurs.
     
  5. lubba64_unity

    lubba64_unity

    Joined:
    Sep 16, 2020
    Posts:
    9
    sorry for the wall of text its a simple question which can easily have a complex answer
     
  6. kdawgfsho04

    kdawgfsho04

    Joined:
    Aug 3, 2020
    Posts:
    18
    THanks for the reply... i messaged you on discord. :)
     
  7. lubba64_unity

    lubba64_unity

    Joined:
    Sep 16, 2020
    Posts:
    9
    ;) no problem. for anyone else checking this thread here's my inventory system explained in a YouTube video: