Keep in variable memory during a restart

Hi, I did a restart script, I would have liked my variable “myVariableOnScript2” on my script “myScript2”, to remain active when the restart starts, so I tried that, but I must have made an error of syntax.

If someone can tell me where I made a mistake.
If there is a better way to do it.

public class restart : MonoBehaviour
{
private myScript2 _varScript2;

    void Start()
        {
        _varScript2= GameObject.Find("NameGameObject").GetComponent<myScript2>();
        }


public void Restart()
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);

        if (_varScript2.myVariableOnScript2== false)
        {
        _varScript2.myVariableOnScript2 = false 
        }     
    }
}

I tried to replace in my condition:

varScript2.myVariableOnScript2 == false
by

I no longer have an error in my console, but it still doesn’t work.

If anyone has an idea ^^

Here I created a button to Mute the music in my game with a bool, so I guess i shouldn’t use PlayerPrefs, but I had a hard time understanding how to do it.

You are setting a variable to false if and only if it is false, what result do you expect?

I created a button with a bool, at startup is set to true, if I click on my button, it becomes false, I would like the false value of the bool to remain active after restarting SceneManager.LoadScene(SceneManager. GetActiveScene().name);

Whether my bool is true or false is not important, what I’m trying to do is to keep its second state during a restart ^^

  1. Maybe dont reload the UI. By Having the UI in another scene or …
  2. Lets say the you had a static bool muted; Then it would not reset on Scene Load.
1 Like

Why are you setting it to false if it is already false?

     if (_varScript2.myVariableOnScript2== false        )
         _varScript2.myVariableOnScript2 = false
     }

a ; is missing and this won’t do anything.

This is probably what you meant to code:

     if (_varScript2.myVariableOnScript2== true        )
         _varScript2.myVariableOnScript2 = false;
     }

However it won’t work either. You cannot rely on anything that belongs to the Scene since it is reloaded, so you have to use another way, PlayerPrefs is one, or you could use a static class like so:

public static class Global
{
    public static bool myValue = true;
}

Then in Script2:

    void Start()
    {
        myVariableOnScript2 = Global.myValue;
    }

and in Restart:

    public void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);

        if (Global.myValue == true)
        {
            Global.myValue = false;
        }
    }

PlayerPrefs is not required since you don’t want it to persist after you close the application.

1 Like

TheFunnySide
I didn’t know that I could place my UI in another scene, but it seems complicated to me at my level.

I tried in static, is it works perfectly.

It only remains for me to study the static variables I do not know.
Thank you

dlorre

Yes sorry I rewrote my script for the forum, I forgot the ; ^^ but no problem on my original script ^^

I removed my code from the restart script, added static variable, as well as a condition to the void start:

{
...
}```

To keep the state of the buttons, and of ```audioSource.mute```.

Everything works perfectly ^^