So I am new to coding and have been spending weeks trying to get my code to work and finally got something okayish. But this morning I woke up and I have no clue what I did but now when I try to pick up my item in game it doesn't go to my inventory slot anymore. I get an error on cs43 and it says inventoryViewController.OnEnable() is the error guessing it is talking about line 24 in the inventoryViewController script. I will add all my scripts below and please if someone can help me with this I would cry with joy. Object reference not set to an instance of an object on my inventoryViewController
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
public class inventoryViewController : MonoBehaviour
{
[SerializeField] private GameObject _inventoryViewObject;
[SerializeField] private TMP_Text _itemNameText;
[SerializeField] private TMP_Text _itemDescriptionText;
[SerializeField] private PlayerMovement _CharacterTankController;
[SerializeField] private animationStateController _CharacterAnimationController;
[SerializeField] private List<ItemSlot> _slots;
private void OnEnable()
{
EventBus.Instance.onPickUpItem += OnItemPickedUp;
}
private void OnDisable()
{
EventBus.Instance.onPickUpItem -= OnItemPickedUp;
}
private void OnItemPickedUp(ItemData itemData)
{
foreach (var slot in _slots)
{
if (slot.IsEmpty())
{
slot.itemData = itemData;
break;
}
}
}
public void OnItemSeleced(ItemSlot selectedSlot)
{
if (selectedSlot.itemData == null)
{
_itemNameText.ClearMesh();
_itemDescriptionText.ClearMesh();
return;
}
_itemNameText.SetText(selectedSlot.itemData.Name);
_itemDescriptionText.SetText(selectedSlot.itemData.Description[0]);
}
private void Update()
{
if (Input.GetKeyUp(KeyCode.Tab))
{
if (_inventoryViewObject.activeSelf)
{
EventBus.Instance.OpenInventory();
}
else
{
EventBus.Instance.CloseInventory();
}
_CharacterTankController.canMove = _inventoryViewObject.activeSelf;
// SHORTENED WAY
_inventoryViewObject.SetActive(!_inventoryViewObject.activeSelf);
// SECOND METHOD OF DOING THE TOGGLE
// if (_inventoryViewObject.activeSelf == true)
// {
// _inventoryViewObject.SetActive(false);
// }
// else
// {
// }
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EventBus : MonoBehaviour
{
public static EventBus Instance { get; private set; }
public event Action onOpenInventory;
public event Action onCloseInventory;
public event Action<ItemData> onPickUpItem;
public void OpenInventory()
{
onOpenInventory?.Invoke();
}
public void CloseInventory()
{
onCloseInventory?.Invoke();
}
public void PickUpItem(ItemData itemData)
{
onPickUpItem?.Invoke(itemData);
}
private void Awake()
{
Instance = this;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemPickup : MonoBehaviour
{
[SerializeField] private ItemData _itemData;
private void OnTriggerStay(Collider other)
{
if (!other.CompareTag("Player")) return;
if (Input.GetKey(KeyCode.E))
{
EventBus.Instance.PickUpItem(_itemData);
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ItemSlot : MonoBehaviour, ISelectHandler
{
public ItemData itemData;
private inventoryViewController _viewController;
private Image _spawnedItemSprite;
public void OnSelect(BaseEventData eventData)
{
_viewController.OnItemSeleced(this);
Debug.Log("Selected");
}
public bool IsEmpty()
{
return itemData == null;
}
private void OnEnable()
{
_viewController = FindObjectOfType<inventoryViewController>();
if (itemData == null) return;
_spawnedItemSprite = Instantiate<Image>(itemData.sprite, transform.position, Quaternion.identity, transform);
}
private void OnDisable()
{
if (_spawnedItemSprite != null)
{
Destroy(_spawnedItemSprite);
}
}
}
If I need to add my player movement script and animation state controller here do let me know!
I have been checking the code out for many hours now and I can't wrap my head around the problem. Visual Studio is giving 0 errors but unity is and it doesn't work. Am I missing a package in unity? and if so. what package?