Why is my Boolean changing for no reason?

Hello,
I have a script (see below) where once a boolean is set to true there is no way to set it to false. But somehow it keeps getting set to false. It actually gets set to false way more than it gets set to true. I think unity is resetting the value of the boolean to the starting value every time because if you set the starting value to true it is just always true. Any clue why it does this and how to fix it?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public Text SL;
public Text SR;
public int scoreleft=0;
public int scoreright = 0;
public bool slow;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Debug.Log(slow);
if (!slow)
{
Time.timeScale = 1f;
}
if (slow)
{
Time.timeScale = 0.5f;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "scoreleft")
{
scoreright++;
SL.text = scoreright.ToString();
slow = true;
}
if (collision.gameObject.tag == "scoreright")
{
scoreleft++;
SR.text = scoreleft.ToString();
slow = true;
}
}
}

its public variable, so check inspector what is the value its set to? (probably its false by default)

is any other script accessing it? (test making it private)

ps. can make scripts look nicer in forums with:

1 Like

I tried making it private and it does the same thing. I guess I’m not 100% sure it is becoming false again, but the code does not slow the Time.timeScale even when it is true unless I comment out the if statement where I reset the time. timescale.

As said above, edit it to use code-tags as it’s harder to read plain text as code.

1 Like

If you add [SerializeField] before a private variable it allows you to view and make edits to the variable in the inspector. But, it keeps it private so you cannot access or change it from other scripts. Do that so you dont need the Debug.Log line in update.

I would add a Debug.Log line in both the collision if brackets, one for the left and one for right.

As another testing method, you can add an input line in Update to toggle your slow bool true/false and control it very quickly to see if the timescale is actually changing or not.

Last thing, mgear (hamburger man above) asked if you are calling/changing the slow bool (as of your script posted above it still says public) in another script? As you mentioned, “there is no way to set it to false” so something has to be changing it. And why is nothing changing it to false? You will want to do that so the timescale can return to normal.

Are you running the script in multiple places?

If so, then you may have one script setting the timescale to 0.5, and another setting it back to 1.0, during each update loop.

If you are, but you need it to run in multiple places, you could try turning it into a static bool, so that there’s only one version of the bool, shared across all instances of that class, something like:

public static bool slow;

If that fixes it, then you may also want to consider making it into a property, like this:

        private static bool _slow;

        public static bool slow
        {
            get => _slow;
            set
            {
                _slow = value;
                Time.timeScale = value ? 0.5f : 1.0f;
            }
        }

and removing your update code.

That way, the timescale will change only at the point you set that value, and you should save some CPU time by not having to check (and set) it in every update loop, for each instance of that script running.