Cannot change value after change scene on Don't Destroy On Load Object

Hi, I want to change the value on isUsingML and isUsingElement. Well on first load, it can change like usual when the button is clicked, but after change to another scene and come back again to the first scene, the value cannot be change after the button clicked.

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameMode : MonoBehaviour
{
    public static bool isUsingMLAgent;
    public static bool isUsingElement;
    private static GameMode instance;

    public void Awake()
    {
        isUsingElement = true;
        isUsingMLAgent = true;
        DontDestroyOnLoad(this);
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

    public void usingML(bool isUsingML)
    {
        if (instance == null)
        {
            instance = this;
        }
        if (isUsingML)
        {
            isUsingMLAgent = true;
        }
        else
        {
             isUsingMLAgent = false;
        }
    }

    public void usingElement(bool isElement)
    {
        if (instance == null)
        {
            instance = this;
        }
        if (isElement)
        {
             isUsingElement = true;
        }
        else
        {
             isUsingElement = false;
        }
    }
}

Thank you

What do you mean by "the value cannot be changed"? 1. You shouldn't make the isUsingMLAgent and isUsingElement bools static. The bools can be referenced via GameMode.instance.isUsingMLAgent if you need to access them in another class. 2. You also don't need to assign (instance = this ) in any functions except Awake.

–

Well i've tried about your comment before i wrote this code, and it's still not working, and after that, i've tried using static (tbh, i don't know what does it do) and it still doesn't work. And what I try to achieve was using the method usingML and usingElement on my button (so I accessed it on the same class), but it still not working

–

2 Answers

2

How is your button hooked up? Does it try to call the public methods of an object in the scene which has this script? If so, the button is going to lose the reference to the object when it destroys itself in Awake when the scene reloads and instance is no longer null.

well yes, the button is calling the public method usingML and usingElement

–

Of course it doesn’t work, once the scene has changed the reference to the DontDestroyOnLoad object is lost!

You can solve this in multiple ways, the fastest is using an intermediate script:

using UnityEngine;
using UnityEngine.UI;
public class UIElementsHandler : MonoBehaviour
{
    public Button button1, button2;

    private void Start()
    {
        button1.onClick.AddListener(() => { GameMode.instance.usingElement(true); });
        button2.onClick.AddListener(() => { GameMode.instance.usingML(true); });
    }
}

This script can’t be attached on a DontDestroyOnLoad object, it is staying in the Scene where it’s placed in order to link the UI elements with the static objects!

Note that the buttons don’t need to have already events listeners setup since we are doing it on the Start() method of the intermediate script.

thank you so much!

–