Button Data Not Preserving Itself In Next Scene.

Hello everyone, button not triggering events once i move unto the next scene.

Here is the button script:

public void clickCashierButton ()
{
// We could take this opportunity to save the clickCashierButton in a file/UserPrefs
// But depending on implementation, this could ve slow/innefficient.
// On the other hand, if we are cpnstantly saving the data on
// the fly, it means that we pretty never lose anything to a crash.

Cash = Cash + 3000;
UpdateUI ();

}

Please use code tags when you post code on the forum, here’s how to do it: https://discussions.unity.com/t/481379

Also please describe what you’re trying to do exactly and what is the problem, because what you wrote does not make much sense. Did you use DontDestroyOnLoad on something? Or why do you expect that any of the values remains? What values exactly? Where do you store them? Give us more.

2 Likes

Yeah i used DontDestroyOnLoad. The problem is that all data are being saved except the button data. Once i move unto the next scene the Onclick() data would’ve disappeared.

    protected float Cash;
    protected float Bank;
    protected int Age;
    protected int Months;

    protected int MaxMonths = 12;
    protected int MinAge = 18;
    protected int MinMonths = 0;
    protected float MinCash = 2000f;
    protected float MinBank = 20f;
   

    static GameStatus instance;

    public static GameStatus GetInstance(){
        return instance;
    }


    // Use this for initialization
    void Start () {
       
            {
            // "Implementation #1"
            // Load data from PlayerPrefs -- this might be from the
            // previous scene, or maybe even from the previous execution(i.e. saved
            // between quitting and reloading the game)
            //Cash = PlayerPrefs.GetInt("Cash", 0f);
            //Bank = PlayerPrefs.GetInt("Bank", 0f);
            //Age = PlayerPrefs.GetInt("Age", 18);
            //Months = PlayerPrefs.GetInt("Months", 0);

            // We want to be a Singleton (i.e. there should only ever be
            // one GameStatus instance at any given time.)


            if (instance != null) {
                // Someone ELSE is the singleton already.
                // So let's just destroy ourselves before we cause trouble.
                Destroy (this.gameObject);
                return;
            }

            // If we get here, the we are "the one". Let's act like it.
            instance = this;    // We are a Highlander
            GameObject.DontDestroyOnLoad (this.gameObject);     // Become immortal

        }    
        Age = MinAge;
        Months = MinMonths;
        Cash = MinCash;
        Bank = MinBank;

        UpdateUI ();
    }
    void    UpdateUI()
    {
        Months = Months + 1;
        if (Months > MaxMonths)
            Age = Age + 1;
        if (Months > MaxMonths)
            Months = 0;   

    }
    public void NoCash()
    {
        Debug.Log("Not Enough Money");
    }

    public void clickNextMonthButton()
    {
        UpdateUI ();
    }

    public void clickCashierButton ()
    {
        // We could take this opportunity to save the clickCashierButton in a file/UserPrefs
        // But depending on implementation, this could've slow/innefficient.
        // On the other hand, if we are constantly saving the data on
        // the fly, it means that we pretty never lose anything to a crash.

        Cash = Cash + 3000;
        UpdateUI ();

I would rename that to Awake, the initialization better to be on Awake so you can have all of these on Start.

But do the Button part of this DontDestroyOnLoad game object? Because I think you expect that the button stays as well and have the on click function set.

The Button part DestroyOnLoad the game object. The on click function set is always missing when new scene is loaded.

Does the target game object and the script on it part of the DOntDestroyOnLoad?

Yeah.