This problem is just weird… I have a “Shop” scene with a Text displaying the user’s current in-game currency.
The Text is assigned to “txt_money” through the unity editor and works perfectly when you start the game in the Shop scene. But when i change scene and come back to the Shop i get the following error:
MissingReferenceException: The object of type ‘Text’ has been destroyed but you are still trying to access it.
Further I inserted some debug.log’s to see if the object exists when changing scenes and it EXISTS IN UPDATE, but DOES NOT EXIST IN the “Purchase()” method.
:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Shop : MonoBehaviour
{
public int money;
public Text txt_money;
private void Update() {
// txt_money exists here...
Debug.Log(txt_money); // Always prints "Text (UnityEngine.UI.Text)"
}
public void Purchase(int ammount) {
// txt_money does not exist here ?
Debug.Log(txt_money); // Prints "null"
money += ammount;
txt_money.text = money.ToString();
}
}
:
Im not sure what to try to fix it, any suggestions?