0

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?

  • `EventBus.Instance` could be null depending on the script execution order. `Awake` and `OnEnable` are called together for each object, but the order of those objects is not guaranteed (by default). So if `inventoryViewController.OnEnable` is called before `EventBus.Awake`, then `EventBus.Instance` will be null when it is accessed. You could confirm this by putting a `Debug.Log` in `inventoryViewController.OnEnable` and `EventBus.Awake` and see which one runs first. You can fix this by forcing EventBus to have a higher priority in the script execution order, which is in the project settings. –  Apr 04 '23 at 05:29
  • rShepp i love you, i changed the EventBus to have a higher priority and it fixed the issue. It must be that my unity just deleted EventBus from the priority or changed it's priority completely. Thank you so much – LikeAardvark Apr 04 '23 at 05:42
  • No problem at all –  Apr 04 '23 at 05:47

0 Answers0