Text.text doesnt work after scene change

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?

@RewForeN where is the purchase function called? maybe it’s called before the object initialize properly? start by removing it from everywhere and having it on a button, click that button after few seconds if it’s still an not valid maybe assign in the beginning of the function and check what happens next in the editor after following those steps and dealing with the result let me know if things work out.

Still dont know why the original code didnt work but found a work around:

public class Shop : MonoBehaviour
{
    public static Shop Instance {set; get;}

   private void Awake() {
        Instance = this;    
    }
}

:

Then where “Purchase()” is called:

Shop.Instance.Purchase(ammount);