0

I have a problem with my game in Unity. The moment I boot it, through the console, I see this message: NullReferenceException: Object Reference Not Set on an Instance of an Object. I don't know what's possible. Since the game is working properly. The only thing that does not show the contents of the variable. I tested with a Debug to the variable and this takes the content. So it's not null.

My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Managers;
using TMPro;
    
public class StateButtonChipsClub : MonoBehaviour
{
    public GameObject ButtonChipsClub;
    
    public bool isAdmin => TablesManager.Instance.club.isMyClub;
    public int chipsAmount => TablesManager.Instance.club.clubChips;
    
    public void Start()
    {
            
        if (isAdmin != true)
        {
            ButtonChipsClub.SetActive(false);
        } 
        else 
        {
            Debug.Log("CHIPS AMOUNT = "+chipsAmount.ToString());
                
            ButtonChipsClub.GetComponent<TextMeshProUGUI>().text = 
                chipsAmount.ToString();
        }
    }
}
jamesnet214
  • 1,044
  • 13
  • 21
Lorenzo
  • 103
  • 5
  • Does `ButtonChipsClub.GetComponent()` actually return something? – Hans Kesting May 20 '21 at 13:20
  • Welcome to SO. Obviously, your `TablesManager.Instance.club` variable is not being initialized before accessing it's properties. How are you initializing it? – Gleb May 20 '21 at 13:21
  • You should return a text. But it doesn't. – Lorenzo May 20 '21 at 13:22
  • I thought it was already initialized when I made the call. Since with isAdmin I don't feel like it. And using Manager is refence TablesManager. – Lorenzo May 20 '21 at 13:24
  • 1
    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) – Nikolay Gonza May 20 '21 at 14:39

1 Answers1

0

I've managed to solve the problem. In the end I declared the variable above.

public TextMeshProUGUI text;

And then I called it in the else like this:

text.GetComponent<TextMeshProUGUI>().text = chipsAmount.ToString();
Lorenzo
  • 103
  • 5