Bool Always True

I have a Vehicle that runs on batteries and gets charged. I’m finding the “Charging” bool is always true. it won’t even let me uncheck it in the inspector.

using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;


public enum Axel
{
    Front,
    Rear
}

[Serializable]
public struct Wheel
{
    public GameObject model;
    public WheelCollider collider;
    public Axel axel;
}

public class TrainController : MonoBehaviour
{

    [SerializeField]
    private float maxAcceleration = 20.0f;
    [SerializeField]
    private float turnSensitivity = 1.0f;
    [SerializeField]
    private float maxSteerAngle = 45.0f;
    [SerializeField]
    private Vector3 _centerOfMass;
    [SerializeField]
    private List<Wheel> wheels;
    [SerializeField]
    private bool forward;

    private float inputX, inputY;

    private Rigidbody _rb;

    private UIManager _uiManager;
    [SerializeField]
    private float _battery = 100f;
    private float batteryDecreaseRate = 5f;
    private float _maxBattery = 100f;
    [SerializeField]
    private bool _powered;
    private SolarPanel _solarPanel;
    private float _chargeRate;
    private float _batteryCharge;
    [SerializeField]
    private bool _charging;

    private void Start()
    {
        _rb = GetComponent<Rigidbody>();
        _rb.centerOfMass = _centerOfMass;
        forward = false;
        _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
        _solarPanel = GameObject.Find("Solar Panel").GetComponent<SolarPanel>();
        if (_uiManager == null)
        {
            Debug.LogError("The UI Manager is Null.");
        }
        if (_solarPanel == null)
        {
            Debug.LogError("The Solar Panel is Null.");
        }
    }


    private void Update()
    {
        //AnimateWheels();
        GetInputs();

        _chargeRate = _solarPanel.PowerProduct();

        if (_battery <= _maxBattery)
        {
            _batteryCharge += _chargeRate * Time.deltaTime;
            _charging = true;

        }
        else
        {
            _charging = false;
        }
       

        _uiManager.UpdateBattery(_battery);

        if (_battery <= 0)
        {
            _powered = false;
        }
        else
        {
            _powered = true;
            if (_charging == false)
            {
                _battery -= batteryDecreaseRate * Time.deltaTime;
            }
           
        }
    }

    private void LateUpdate()
    {

        if (_powered == true)
        {
             Move();
              //Turn();
        }
       
       
    }

    private void GetInputs()
    {
        inputX = Input.GetAxis("Horizontal");
        inputY = Input.GetAxis("Vertical");
    }

    private void Move()
    {

        if (forward == true)
        {
            foreach (var wheel in wheels)
            {
                wheel.collider.motorTorque = maxAcceleration * 500 * Time.deltaTime;
            }
        }
    }

    //private void Turn()
    //{

    //    if (forward == true) {
    //    foreach (var wheel in wheels)
    //    {
    //        if (wheel.axel == Axel.Front)
    //        {
    //            var _steerAngle = inputX * turnSensitivity * maxSteerAngle;
    //            wheel.collider.steerAngle = Mathf.Lerp(wheel.collider.steerAngle, _steerAngle, 0.5f);
    //        }
    //    }
    //    }
       
    //}

    //private void AnimateWheels()
    //{
    //    foreach (var wheel in wheels)
    //    {
    //        Quaternion _rot;
    //        Vector3 _pos;
    //        wheel.collider.GetWorldPose(out _pos, out _rot);
    //        wheel.model.transform.position = _pos;
    //        wheel.model.transform.rotation = _rot;
    //    }
    //}
}

You are setting it to true in your update method, if battery < maxBattery.

Since nothing in your code ever increments battery (or decrements maxBattery), then your code will keep setting Charging to true, and there is no way that it can ever set Charging to false.

1 Like

Due to having nothing decrement or increment the maxBattery it will always be true

Bcoz battery is set to 100f and so is max battery
This statement if(battery <= maxbattery) will always be true with nothing to change it.

I got it working! thanks, guys. I was also overcomplicating it. It does that I’m looking for now.