I have two function that use a UIDocument Label
public class UIController : MonoBehaviour
{
private Label argentTexte;
private int currentInt;
private string currentString;
void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
argentTexte = root.Q<Label>("currentMoney");
Debug.Log(argentTexte.text); //<- show some string
}
public void IncreaseMoney(int moneyToAdd)
{
currentString = GetComponent<UIDocument>().rootVisualElement.Q<Label>("currentMoney").text; // Don't show anything, throw error
Debug.Log(argentTexte.text); //throw an NullReferenceException: Object reference not set to an instance of an object
int.TryParse(currentString, out currentInt);
argentTexte.text = $"{currentInt + moneyToAdd}";
}
public void DecreaseMoney(int moneyToDecrease)
{
currentString = GetComponent<UIDocument>().rootVisualElement.Q<Label>("currentMoney").text; // show some string
int.TryParse(currentString, out currentInt);
argentTexte.text = $"{currentInt - moneyToDecrease}";
}
}
DecreaseMoney() is used on an object when i spawn it and it work, IncreaseMoney() is used on a child of an object when its destroyed and doesn't work.
Why DecreaseMoney() have access to argentTexte.text and not IncreaseMoney() ?