I have created an inventory system, following a great tutorial I found on Youtube using scriptable objects :Unity INVENTORY: A Definitive Tutorial - YouTube
What I want to do is figure out if the item in my currently selected slot has a certain name (eg “Pickaxe”), and if so enable the item under my player using .SetActive.
Here is my code:
INVENTORY MANAGER SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
public static InventoryManager instance;
public Item startItem;
public InventorySlot[] inventorySlots;
public GameObject inventoryItemPrefab;
public int maxStackAmount;
int selectedSlot = -1;
private void Awake ()
{
instance = this;
}
private void Start ()
{
ChangeSelectedSlot(0);
AddItem(startItem);
}
private void Update ()
{
// Keyboard
if (Input.inputString != null)
{
bool isNumber = int.TryParse(Input.inputString, out int number);
if (isNumber && number > 0 && number < 9)
{
ChangeSelectedSlot(number -1);
}
}
// Scrolling
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll != 0)
{
int newValue = selectedSlot + (int)(scroll / Mathf.Abs(scroll));
if (newValue < 0)
{
newValue = 7;
}
else if (newValue >= 8)
{
newValue = 0;
}
ChangeSelectedSlot(newValue);
}
}
void ChangeSelectedSlot (int newValue)
{
if (selectedSlot >= 0)
{
inventorySlots[selectedSlot].Deselect();
}
inventorySlots[newValue].Select();
selectedSlot = newValue;
}
public bool AddItem (Item item)
{
// Check if any slots have same item with count lower than max
for (int i = 0; i < inventorySlots.Length; i++)
{
InventorySlot slot = inventorySlots[i];
InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemInSlot != null && itemInSlot.item == item && itemInSlot.count < maxStackAmount && itemInSlot.item.stackable == true)
{
itemInSlot.count++;
itemInSlot.RefreshCount();
return true;
}
}
// Find empty slot
for (int i = 0; i < inventorySlots.Length; i++)
{
InventorySlot slot = inventorySlots[i];
InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemInSlot == null)
{
SpawnNewItem(item, slot);
return true;
}
}
return false;
}
void SpawnNewItem (Item item, InventorySlot slot)
{
GameObject newItemGo = Instantiate(inventoryItemPrefab, slot.transform);
InventoryItem inventoryItem = newItemGo.GetComponent<InventoryItem>();
inventoryItem.InitialiseItem(item);
}
public Item GetSelectedItem (bool use)
{
InventorySlot slot = inventorySlots[selectedSlot];
InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemInSlot != null)
{
Item item = itemInSlot.item;
if (use)
{
itemInSlot.count--;
if (itemInSlot.count <= 0)
{
Destroy(itemInSlot.gameObject);
} else
{
itemInSlot.RefreshCount();
}
}
return item;
}
return null;
}
}
ITEM SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
[CreateAssetMenu(menuName = "Scriptable object/Item")]
public class Item : ScriptableObject
{
[Header("Only gameplay")]
public TileBase tile;
public ItemType type;
public ActionType actionType;
public Vector2Int range = new Vector2Int(5, 4);
[Header("Only UI")]
public bool stackable = true;
[Header("Both")]
public Sprite image;
public string name;
}
public enum ItemType
{
BuildingBlock,
Tool
}
public enum ActionType
{
Dig,
Mine
}
SCRIPT ATTACHED TO PLAYER TO ENABLE/DISABLE PICKAXE
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInventoryManager : MonoBehaviour
{
public GameObject pickaxe;
void Start()
{
pickaxe.SetActive(false);
}
void Update()
{
Item recievedItem = InventoryManager.instance.GetSelectedItem(false);
if (recievedItem.name == "Pickaxe") // We are holding pickaxe
{
pickaxe.SetActive(true);
} else if (recievedItem.name != "Pickaxe")
{
pickaxe.SetActive(false);
}
}
}