When I walk over an item and pick it up, I go to my inventory and click on it to equip it. I don’t know why it won’t equip though. Does anyone know what I did wrong? It seems right to me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item : MonoBehaviour
{
public int ID;
public string type;
public string description;
public Sprite icon;
public bool pickedUp;
[HideInInspector]
public bool equipped;
[HideInInspector]
public GameObject weapon;
[HideInInspector]
public GameObject weaponManager;
public bool playersWeapon;
public void Start()
{
weaponManager = GameObject.FindWithTag("WeaponManager");
if(!playersWeapon)
{
int allWeapons = weaponManager.transform.childCount;
for (int i = 0; i < allWeapons; i++)
{
if(weaponManager.transform.GetChild(i).gameObject.GetComponent<Item>().ID == ID)
{
weapon = weaponManager.transform.GetChild(i).gameObject;
print(weapon.name);
}
}
}
}
public void Update()
{
if(equipped)
{
//perform weapon acts here
if (Input.GetKeyDown(KeyCode.Alpha1))
equipped = false;
if (equipped == false)
this.gameObject.SetActive(false);
}
}
public void ItemUsage()
{
//weapon
if(type == "Weapon")
{
weapon.SetActive(true);
equipped = true;
}
//health
//water
//food
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Slots : MonoBehaviour, IPointerClickHandler
{
public GameObject item;
public int ID;
public string type;
public string description;
public bool empty;
public Transform slotIconGO;
public Sprite icon;
public void OnPointerClick(PointerEventData pointerEventData)
{
UseItem();
}
private void Start()
{
slotIconGO = transform.GetChild(0);
}
public void UpdateSlot()
{
this.GetComponent<Image>().sprite = icon;
}
public void UseItem()
{
item.GetComponent<Item>().ItemUsage();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
private bool inventoryEnabled;
public GameObject inventory;
private int allSlots;
private int enabledSlots;
private GameObject[] slot;
public GameObject slotHolder;
void Start()
{
allSlots = 30;
slot = new GameObject[allSlots];
for(int i = 0; i < allSlots; i++)
{
slot[i] = slotHolder.transform.GetChild(i).gameObject;
if (slot[i].GetComponent<Slots>().item == null)
slot[i].GetComponent<Slots>().empty = true;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
inventoryEnabled = !inventoryEnabled;
if (inventoryEnabled == true)
{
inventory.SetActive(true);
}
else
{
inventory.SetActive(false);
}
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Item")
{
GameObject itemPickedUp = other.gameObject;
Item item = itemPickedUp.GetComponent<Item>();
AddItem(itemPickedUp, item.ID, item.type, item.description, item.icon);
}
}
void AddItem(GameObject itemObject, int itemID, string itemType, string itemDescription, Sprite itemicon)
{
for(int i = 0; i < allSlots; i++)
{
if(slot[i].GetComponent<Slots>().empty)
{
//add item to slot
itemObject.GetComponent<Item>().pickedUp = true;
slot[i].GetComponent<Slots>().item = itemObject;
slot[i].GetComponent<Slots>().icon = itemicon;
slot[i].GetComponent<Slots>().type = itemType;
slot[i].GetComponent<Slots>().ID = itemID;
slot[i].GetComponent<Slots>().description = itemDescription;
itemObject.transform.parent = slot[i].transform;
itemObject.SetActive(false);
slot[i].GetComponent<Slots>().UpdateSlot();
slot[i].GetComponent<Slots>().empty = false;
}
return;
}
}
}