So I followed some YouTube videos to create an inventory system and I’m currently tweaking them to fit my needs. One need of mine is to find out if a key is in my inventory to be able to open a door. I already know the function I need to check if the trigger and key are pressed, but I also need to know if I have the key. Could someone help me? Thank you for your time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using CodeMonkey.Utils;
public class UI_Inventory : MonoBehaviour
{
private Inventory inventory;
private Transform itemSlotContainer;
private Transform itemSlotTemplate;
private PlayerUpDown player;
public void Awake()
{
itemSlotContainer = transform.Find("itemSlotContainer");
itemSlotTemplate = itemSlotContainer.Find("itemSlotTemplate");
}
public void SetPlayer(PlayerUpDown player)
{
this.player = player;
}
public void SetInventory(Inventory inventory)
{
this.inventory = inventory;
inventory.OnItemListChanged += Inventory_OnItemListChanged;
RefreshInventoryItems();
}
private void Inventory_OnItemListChanged(object sender, System.EventArgs e)
{
RefreshInventoryItems();
}
private void RefreshInventoryItems()
{
foreach (Transform child in itemSlotContainer)
{
if (child == itemSlotTemplate) continue;
Destroy(child.gameObject);
}
int x = 0;
int y = 0;
float itemSlotCellSize = 100f;
foreach (Item item in inventory.GetItemList())
{
RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<RectTransform>();
itemSlotRectTransform.gameObject.SetActive(true);
itemSlotRectTransform.GetComponent<Button_UI>().ClickFunc = () =>
{
};
itemSlotRectTransform.GetComponent<Button_UI>().MouseRightClickFunc = () =>
{
// Drop item
inventory.RemoveItem(item);
ItemWorld.DropItem(player.GetPosition(), item);
};
itemSlotRectTransform.anchoredPosition = new Vector2(x * itemSlotCellSize, y * itemSlotCellSize + 100);
Image image = itemSlotRectTransform.Find("image").GetComponent<Image>();
image.sprite = item.GetSprite();
x++;
if (x > 4)
{
x = 0;
y++;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class Item
{
public enum ItemType
{
Red,
Blue,
Key,
}
public ItemType itemType;
public int amount;
public Sprite GetSprite()
{
switch (itemType)
{
default:
case ItemType.Red: return ItemAssets.Instance.redItem;
case ItemType.Blue: return ItemAssets.Instance.blueItem;
case ItemType.Key: return ItemAssets.Instance.keyItem;
}
}
public bool IsStackable()
{
switch (itemType)
{
default:
case ItemType.Red:
case ItemType.Blue:
return true;
case ItemType Key:
return false;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Inventory
{
public event EventHandler OnItemListChanged;
private List<Item> itemList;
public Inventory()
{
itemList = new List<Item>();
}
public void AddItem(Item item)
{
if (item.IsStackable())
{
bool itemAlreadyInInventory = false;
foreach (Item inventoryItem in itemList)
{
if (inventoryItem.itemType == item.itemType)
{
inventoryItem.amount += item.amount;
itemAlreadyInInventory = true;
}
}
if (!itemAlreadyInInventory)
{
itemList.Add(item);
}
}
else
{
itemList.Add(item);
}
OnItemListChanged?.Invoke(this, EventArgs.Empty);
}
public List<Item> GetItemList()
{
return itemList;
}
public void RemoveItem(Item item)
{
if (item.IsStackable())
{
Item itemInInventory = null;
foreach (Item inventoryItem in itemList)
{
if (inventoryItem.itemType == item.itemType)
{
inventoryItem.amount -= item.amount;
itemInInventory = inventoryItem;
}
}
if (itemInInventory != null && itemInInventory.amount <= 0)
{
itemList.Remove(itemInInventory);
}
}
else
{
itemList.Remove(item);
}
OnItemListChanged?.Invoke(this, EventArgs.Empty);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerUpDown : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
Vector2 movement;
private Inventory inventory;
[SerializeField] private UI_Inventory uiInventory;
private void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.deltaTime);
}
private void Awake()
{
{
inventory = new Inventory();
uiInventory.SetInventory(inventory);
uiInventory.SetPlayer(this);
}
}
private void OnTriggerStay2D(Collider2D collider)
{
Debug.Log("This is called every frame.");
ItemWorld itemWorld = collider.GetComponent<ItemWorld>();
if (itemWorld != null && InputKeyPressed())
{
inventory.AddItem(itemWorld.GetItem());
itemWorld.DestroySelf();
}
else if (collider.CompareTag("Door") && InputKeyPressed())
{
Debug.Log("Door is unlocked.");
SceneManager.LoadScene(1);
}
}
private bool InputKeyPressed()
{
return Input.GetKeyDown(KeyCode.E);
}
public Vector3 GetPosition()
{
return transform.position;
}
}