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.
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);
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;
}