- Home /
Bool not changing on button press
So I have a problem with my menu screen and its where my bool values will not change. The button press does but the bool just stays the same. I'm really confused. Here is my code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class MenuScreenScript : MonoBehaviour
{
private bool original = false;
private bool maze = false;
private bool blank = false;
private bool casual = false;
private bool hard = false;
public void OriginalButtonPress()
{
original = true;
Debug.Log("yeet");
}
public void MazeButtonPress()
{
maze = true;
}
public void BlankButtonPress()
{
blank = true;
}
public void CasualButtonPress()
{
casual = true;
}
public void HardButtonPress()
{
hard = true;
}
private void Update()
{
Debug.Log(original);
if (original == true)
{
if (casual == true)
{
SceneManager.LoadScene("MainScene");
}
}
}
}
I tested all of the buttons. Its only the value that wont change.
How do you know that the on button press logic triggers? Seems like this should work if OriginalButtonPress() and CasualButtonPress() are triggered... psst... you can debug your project and see what lines are triggered and which aren't.
I tested all of the buttons. Its only the value that wont change.
Hey there! Is your bool "casual" being set to true somewhere else? Also it would help to see your button in the inspector. You're saying you get that Debug log "yeet"?
No for the first question, and I put that Debug.Log thing there to see if the button was working. Its nothing to do with the bool values its the button. I hooked up the script to the on press and put in the function. I have an event system. When I press the button nothing happens. It does the colour changing to a darker grey but the value does not change and niether does the Debug statement.
Answer by j4ke · Feb 07, 2020 at 10:01 AM
Are the buttons supposed to toggle between those Boolean values? Like between casual and hard? Then the code could be like this:
public void CasualButtonPress()
{
casual = true;
hard = false;
}
public void HardButtonPress()
{
casual = false;
hard = true;
}
Or just in a simpler way, if casual is meant to be the default game difficulty or such:
private bool isHard = false;
public void CasualButtonPress()
{
isHard = false;
}
public void HardButtonPress()
{
isHard = true;
}
They are not supposed to toggle between the two, its supposed to change the scene as soon as the button is pressed. The buttons all work because I tested with Debug.Log. Its only the value that wont change for some reason.
You say "the buttons all work" but all they do is change those boolean values... so do they change them or not?
Or is it that the boolean values are reset when the new scene is loaded? If so then (unless this is on a DontDestroyOnLoad object) it'll be because in the new scene there's a new menu with all its fields at their initial values. If you want a changed value to persist between scene loads you could consider
1. making it a static variable
2. getting it from a persistent object (some kind of manager with DontDestroyOnLoad set)
3. saving the mode to playerprefs before the switch and then loading it in the Awake() function (which would enable your users to start up in whatever mode they were using the last time they quit)
Your answer
![](https://koobas.hobune.stream/wayback/20220612222900im_/https://answers.unity.com/themes/thub/images/avi.jpg)