- Home /
how to detect wich item i have in my inevntory
i found a video on youtube wich showed me how to make a inventory system. I made some changes but now i dont understand how to check wich items are in my inventory. In my ItemObj scrip i tried the method
"if (this.GetComponent().sprite.name == "Weapon")"
but if i try to use something like a campfire it gets a null refrence exception. Can somebody help me to find out how to detect wich items i have in my inventory?
temDataBase script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemDataBase : MonoBehaviour
{
public List<Item> items = new List<Item>();
void Awake()
{
GetItemByID();
}
void GetItemByID()
{
items.Add(new Item("Glock", "Waffe", "Hoffentlich", 0));
items.Add(new Item("M4A1", "Ein 8 Kaliber Gewehr", "Weapon", 1));
}
public Item GetItemByID(int id)
{
foreach (Item itm in items)
{
if(itm.itemId == id)
{
return itm;
}
}
return null;
}
}
Item scirpt:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Item {
public int itemId;
public string itemDescription;
public Sprite itemSprite;
public string itemName;
public string itemSpriteName;
public Item(string name, string description, string spriteName, int id)
{
itemName = name;
itemDescription = description;
itemSpriteName = spriteName;
itemSprite = Resources.Load<Sprite>("ItemIcons/" + spriteName);
itemId = id;
}
public Item()
{
}
}
Inventory script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public int slotAmount = 11;
public GameObject slotPrefab;
public GameObject slotParent;
public GameObject InventoryObject;
public GameObject itemPrefab;
private MouseLook mouseLook;
private ToolTip tooltip;
public List<GameObject> slots = new List<GameObject>();
private ItemDataBase itemDatabase;
public KeyCode inventorykey = KeyCode.Tab;
public bool isShown = false;
// Start is called before the first frame update
void Start()
{
CreateSlots(slotAmount);
mouseLook = GameObject.FindObjectOfType<MouseLook>();
itemDatabase = GameObject.FindGameObjectWithTag("ItemDataBase").GetComponent<ItemDataBase>();
tooltip = GameObject.FindGameObjectWithTag("Tooltip").GetComponent<ToolTip>();
}
void Update()
{
if (Input.GetKeyDown(inventorykey))
{
isShown = !isShown;
if(isShown == true)
{
Cursor.visible = true;
tooltip.ToggleTooltip(false);
Cursor.lockState = CursorLockMode.None;
mouseLook.mouseSensitivity = 0f;
}
else if (isShown == false)
{
Cursor.lockState = CursorLockMode.Locked;
mouseLook.mouseSensitivity = 900f;
Cursor.visible = false;
}
}
if(isShown == true)
{
InventoryObject.SetActive(true);
}
else if(isShown == false)
{
InventoryObject.SetActive(false);
}
}
// Update is called once per frame
void CreateSlots(int slotAmount)
{
for (int i = 0; i < slotAmount; i++)
{
slots.Add(Instantiate(slotPrefab));
slots[i].transform.SetParent(slotParent.transform);
}
}
public void AddItem(int id)
{
Item itemAdd = itemDatabase.GetItemByID(id);
for (int i = 0; i < slots.Count; i++)
{
if(slots[i].transform.childCount <= 0)
{
GameObject itemInstance = Instantiate(itemPrefab);
itemInstance.transform.SetParent(slots[i].transform);
itemInstance.transform.localPosition = Vector2.zero;
itemInstance.GetComponent<Image>().sprite = itemAdd.itemSprite;
break;
}
}
}
}
ItemObj script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ItemObj : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
{
private Transform startParent;
private CanvasGroup canvasGroup;
private Campfire campfire;
private Item item;
private PickUp pickUp;
public Inventory inventory;
private ItemDataBase itemDataBase;
private ToolTipButton toolTipB;
private ToolTip tooltip;
private ToolTipButton toolTipButton1;
private ToolTipButton toolTipButton2;
private Food foodItem;
private PlayerMovement playerMovement;
private Interact interact;
private bool inItem = false;
private void Start()
{
interact = GameObject.FindObjectOfType<Interact>();
foodItem = GameObject.FindObjectOfType<Food>();
playerMovement = GameObject.FindObjectOfType<PlayerMovement>();
inventory = GameObject.FindObjectOfType<Inventory>();
canvasGroup = GetComponent<CanvasGroup>();
toolTipB = GameObject.FindObjectOfType<ToolTipButton>();
pickUp = GameObject.FindObjectOfType<PickUp>();
tooltip = GameObject.FindGameObjectWithTag("Tooltip").GetComponent<ToolTip>();
toolTipButton1 = GameObject.FindGameObjectWithTag("TooltipButton1").GetComponent<ToolTipButton>();
toolTipButton2 = GameObject.FindGameObjectWithTag("TooltipButton2").GetComponent<ToolTipButton>();
startParent = transform.parent;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse1) && inItem == true)
{
tooltip.ToggleTooltip(true, Input.mousePosition);
toolTipButton1.actionMethod = UseItemButton;
toolTipButton2.actionMethod = DropViaItemButton;
}
Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider.tag == "Campfire")
{
if (Input.GetKeyDown(KeyCode.E))
{
if (this.GetComponent<Image>().sprite.name == "Weapon")
{
Destroy(gameObject);
campfire = hitInfo.collider.GetComponent<Campfire>();
inventory.AddItem(0);
}
}
}
}
}
private void UseItemButton()
{
if (this.GetComponent<Image>().sprite.name == "Hoffentlich")
{
Debug.Log("you used it");
useItemTwo();
}
if (this.GetComponent<Image>().sprite.name == "Weapon")
{
Debug.Log("you used it");
useItem();
}
}
private void DropViaItemButton()
{
if (this.GetComponent<Image>().sprite.name == "Hoffentlich")
{
DropItem();
Debug.Log("You dropped your item!");
}
if (this.GetComponent<Image>().sprite.name == "Weapon")
{
DropItem();
Debug.Log("You dropped your item!");
}
}
void useItem()
{
interact.TestHunger2();
Destroy(gameObject);
}
void useItemTwo()
{
interact.TestHunger();
Destroy(gameObject);
}
void DropItem()
{
Destroy(gameObject);
}
public void OnBeginDrag(PointerEventData eventData)
{
transform.position = eventData.position;
transform.SetParent(transform.parent.parent);
canvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
GetSlotInfo();
canvasGroup.blocksRaycasts = true;
}
private void GetSlotInfo()
{
GameObject[] slots = GameObject.FindGameObjectsWithTag("Slot");
foreach (var slot in slots)
{
if(slot.GetComponent<Slot>().isSelected == true)
{
if(slot.transform.childCount > 0)
{
ItemObj otherItem = slot.transform.GetChild(0).GetComponent<ItemObj>();
Transform otherItemParent = otherItem.startParent;
otherItem.startParent = startParent;
startParent = otherItemParent;
otherItem.transform.SetParent(otherItem.startParent);
otherItem.transform.localPosition = Vector2.zero;
}
else
{
startParent = slot.transform;
}
break;
}
}
transform.SetParent(startParent);
transform.localPosition = Vector2.zero;
}
public void OnPointerEnter(PointerEventData eventData)
{
inItem = true;
}
public void OnPointerExit(PointerEventData eventData)
{
inItem = false;
}
}
i would be really gratefull if somebody could help me
Comment