- Home /
Question on timeScale and boolean script.
I have code where when the the player crashes, the timeScale turns to 0 which stops the game and brings up some buttons. On one of the buttons I want to implement it so that once it is pressed, it disappears and cannot be pressed again. I set a boolean so that once timescale = 0 then it is set to true and the button shows. Inside the button if statement, it turns the boolean false and should make it disappear. Problem is, the timescale is still 0 which in turn always makes the boolean true which always shows the button. Sounds a bit confusing, here is the script:
private bool showbutton == false;
if (time.timescale ==0)
{
showbutton = true;
}
if(showbutton){
if(GUI.button){
showbutton = false;
}
private bool showbutton == false;
That is an Incorrect assignment statement.
== is an evaluation not an assignment.
private bool showbutton = false;
private bool showButton = false;
// Update is called once per frame
void Update () {
Debug.Log(showButton);
Debug.Log(Time.timeScale);
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 200, 30), "Stop Time"))
{
Time.timeScale = 0;
showButton = true;
}
if (showButton)
{
if (GUI.Button(new Rect(10, 50, 200, 30), "Start Time"))
{
Time.timeScale = 1;
showButton = false;
}
}
}
Does that do what your asking?
Thanks for the reply but that is unfortunately not what I wanted to do. I have only one equal sign in my script it is just on a different computer so I was typing it from memory. The problem with your script is that for my game, when the player dies then that collision makes timescale = 0. $$anonymous$$y menu is triggered when timescale = 0 as in:
if (time.timescale ==0){
showbutton = true;
}
So you see, if I make showbutton = false with more code, timescale is still at 0 which always make the showbutton = true.
You need to add another Variable that detects if you have made a collision already and use that Variable where you assign Time.timeScale to 0.
Consider the following Code on the concept.
private bool showButton = false;
private bool hasCollided = false;
// Update is called once per frame
void Update () {
Debug.Log(showButton);
Debug.Log(Time.timeScale);
if (Time.timeScale == 0)
{
showButton = true;
}
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 200, 30), "Simulate Collision") && !hasCollided) // After we have already collided once we can no longer enter this statement until hasCollided is false again
{
Time.timeScale = 0;
hasCollided = true; // The assignment that tells us we have already collided at least once.
}
if (showButton)
{
if (GUI.Button(new Rect(10, 50, 200, 30), "Start Time"))
{
Time.timeScale = 1;
showButton = false;
}
}
}
Obviously where i have used the Button that says "Simulate Collision" would be where you detect that a collision has occured. Im only using this Button for the purpose of this Example.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134004im_/https://answers.unity.com/themes/thub/images/avi.jpg)