Getting null reference from a shopping system

I'm once again on StackOverflow asking for help because I'm getting an error I can't wrap my head around. For Context, I'm pretty new to Unity, so I've followed a tutorial. Here is the link: [https://www.youtube.com/watch?v=Oie-G5xuQNA] And for easier access to the code I will also be posting it here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonInfo : MonoBehaviour
{
    public int ItemID;
    public Text PriceText;
    public Text QuantityText;
    public GameObject ShopManager;

    // Update is called once per frame
    void Update()
    {
        PriceText.text = "Price: $" + ShopManager.GetComponent<ShopManagerScript>().shopItems[2, ItemID].ToString();
        QuantityText.text = ShopManager.GetComponent<ShopManagerScript>().shopItems[3, ItemID].ToString();

    }
}

And also here is the second script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ShopManagerScript : MonoBehaviour
{
    public int [,] shopItems = new int[5,5];
    public float coins;
    public Text CoinsText;


    void Start()
    {
        CoinsText.text = "Coins" + coins.ToString();

        // ID's
        shopItems[1, 1] = 1;
        shopItems[1, 2] = 2;
        shopItems[1, 3] = 3;
        shopItems[1, 4] = 4;

        // Price
        shopItems[2, 1] = 10;
        shopItems[2, 2] = 20;
        shopItems[2, 3] = 30;
        shopItems[2, 4] = 40;

        // Quantity
        shopItems[3, 1] = 1;
        shopItems[3, 2] = 1;
        shopItems[3, 3] = 1;
        shopItems[3, 4] = 1;


    }

    public void Buy()
    {
        GameObject ButtonRef = GameObject.FindGameObjectWithTag("Event").AddComponent<EventSystem>().currentSelectedGameObject;

        if(coins >= shopItems[2, ButtonRef.GetComponent<ButtonInfo>().ItemID])
        {
            coins -= shopItems[2, ButtonRef.GetComponent<ButtonInfo>().ItemID];
            shopItems[3, ButtonRef.GetComponent<ButtonInfo>().ItemID]++;
            CoinsText.text = "Coins" + coins.ToString();
            ButtonRef.GetComponent<ButtonInfo>().QuantityText.text = shopItems[3, ButtonRef.GetComponent<ButtonInfo>().ItemID].ToString();
        }
    }
}

And the last thing: Here is the error message itself: NullReferenceException: Object reference not set to an instance of an object ShopManagerScript.Buy () (at Assets/Scrips/UI/Shop System/ShopManagerScript.cs:43)

Sorry if it was kinda messy pasting in so much code, but I just want to get my question answered. So, if anyone would help me, it would be appretiated a lot! :)


Solution 1:

It looks like in your Buy() method you are calling AddComponent to add an EventSystem to your object tagged as "Event". Most likely this object already has an EventSystem so it cannot add another and that is providing a null reference to your ButtonRef object. You most likely want to be calling GetComponent there instead to get the already existing EventSystem component