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.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UI;
public class gameManager : MonoBehaviour
{
public static gameManager instance;
public bool isPaused;
public List<Item> items = new List<Item>();// WHAT KIND OF ITEMS
public List<int> itemNumbers = new List<int>();//HOW MANY WE HAVE
public GameObject[] slots;
//public.Dictionary<Item, int> itemDict = new Dictionary<Item, int>();//OPTIONAL
//public Item addItem_01;//TO DO REMOVE LATER
//public Item removeItem_01;//TO DO REMOVE LATER
public ItemButton thisButton;//KEEP TRACK OF WHICH ITEM WE ARE HOVERING OVER
public ItemButton[] itemButtons;//ALL THE ITEM BUTTONS IN GAME[USED FOR RESET]
private void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
if (instance != this)
{
Destroy(gameObject);
}
}DontDestroyOnLoad(gameObject);
}
public void Start()
{
DisplayItems();
}
private void DisplayItems()
{
#region
/*for (int i = 0; i < items.Count; i++)
{
//UPDATE SLOTS ITEM IMAGE
slots.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 1f);
slots.transform.GetChild(0).GetComponent<Image>().sprite = items.itemSprite;
//UPDATE SLOTS COUNT TEXT
slots.transform.GetChild(1).GetComponent<Text>().color = new Color(1f, 1f, 1f, 1f);
slots.transform.GetChild(1).GetComponent<Text>().text = itemNumbers.ToString();
//UPDATE CLOSE/THROW BUTTON
slots.transform.GetChild(2).gameObject.SetActive(true);
}*/
#endregion
//WE IGNORE THE FACT
for (int i = 0; i < slots.Length; i++)
{
if (i < items.Count)
{
//UPDATE SLOTS ITEM IMAGE
slots.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 1f);
slots.transform.GetChild(0).GetComponent<Image>().sprite = items.itemSprite;
//UPDATE SLOTS COUNT TEXT
slots.transform.GetChild(1).GetComponent<Text>().color = new Color(1f, 1f, 1f, 1f);
slots.transform.GetChild(1).GetComponent<Text>().text = itemNumbers.ToString();
//UPDATE CLOSE/THROW BUTTON
slots.transform.GetChild(2).gameObject.SetActive(true);
}
else//SOME REMOVE ITEMS
{
//UPDATE SLOTS ITEM IMAGE
slots.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 0f);
slots.transform.GetChild(0).GetComponent<Image>().sprite = null;
//UPDATE SLOTS COUNT TEXT
slots.transform.GetChild(1).GetComponent<Text>().color = new Color(1f, 1f, 1f, 0f);
slots.transform.GetChild(1).GetComponent<Text>().text = null;
//UPDATE CLOSE/THROW BUTTON
slots.transform.GetChild(2).gameObject.SetActive(false);
}
}
}
private void Update()
{/*
if (Input.GetKeyDown(KeyCode.M))
{
AddItem(addItem_01);
}
if (Input.GetKeyDown(KeyCode.N))
{
RemoveItem(removeItem_01);
}*/
}
public void AddItem(Item _item)
{
//MARKER IF THERE IS ONE EXISTING ITEM IN OUR BAG/LIST
if (!items.Contains(_item))
{
items.Add(_item);
itemNumbers.Add(1);//ADD ONE
}
//IF THERE IS A NEW _ITEM IN OUR BAG
else
{
UnityEngine.Debug.Log("You Already Have This One.");
for (int i = 0; i < items.Count; i++)
{
if (_item == items)
{
itemNumbers[i]++;
}
}
}
DisplayItems();
}
public void RemoveItem(Item _item)
{
//IF ONE IS EXISTING IN OUR INV
if (items.Contains(_item))
{
for (int i = 0; i < items.Count; i++)
{
if (_item == items[i])
{
itemNumbers[i]--;
if (itemNumbers[i] == 0)
{
//WE HAVE TO REMOVE THIS ITEM
items.Remove(_item);
itemNumbers.Remove(itemNumbers[i]);
}
}
}
}
else
{
UnityEngine.Debug.Log("THERE IS NO" + _item + "IN YOUR INVENTORY");
}
//IF THERE IS NO OTHER ITEN IN INV
ResetButtonItems();
DisplayItems();
}
public void ResetButtonItems()
{
for (int i = 0; i < itemButtons.Length; i++)//FOR LOOP OF ALL BUTTONS TOTAL IS 14
{
if (i < items.Count)
{
itemButtons[i].thisItem = items[i];
}
else
{
itemButtons[i].thisItem = null;
}
}
}
public void Use()
{
//Item.Use();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "Item", fileName = "New Item")]
public class Item : ScriptableObject
{
public string itemName;
public string itemDes;
public Sprite itemSprite;
public int itemPrice;
public virtual void Use()
{
}