0

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)

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Which line is 43 in `ShopManagerScript`? Is it `if(coins >= shopItems[2, ButtonRef.GetComponent().ItemID])`? If so, there's probably no `ButtonInfo` attached to `ButtonRef`, and so the `GetComponent()` call is returning null, and the exception is caused by the `.ItemID` operation. – Ruzihm Jan 10 '22 at 22:54
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder Jan 10 '22 at 22:55

1 Answers1

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

Boxfriend
  • 76
  • 3