Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Bool Always True

Discussion in 'Scripting' started by Grivik, Jul 7, 2020.

  1. Grivik

    Grivik

    Joined:
    Apr 22, 2020
    Posts:
    5
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public enum Axel
    8. {
    9.     Front,
    10.     Rear
    11. }
    12.  
    13. [Serializable]
    14. public struct Wheel
    15. {
    16.     public GameObject model;
    17.     public WheelCollider collider;
    18.     public Axel axel;
    19. }
    20.  
    21. public class TrainController : MonoBehaviour
    22. {
    23.  
    24.     [SerializeField]
    25.     private float maxAcceleration = 20.0f;
    26.     [SerializeField]
    27.     private float turnSensitivity = 1.0f;
    28.     [SerializeField]
    29.     private float maxSteerAngle = 45.0f;
    30.     [SerializeField]
    31.     private Vector3 _centerOfMass;
    32.     [SerializeField]
    33.     private List<Wheel> wheels;
    34.     [SerializeField]
    35.     private bool forward;
    36.  
    37.     private float inputX, inputY;
    38.  
    39.     private Rigidbody _rb;
    40.  
    41.     private UIManager _uiManager;
    42.     [SerializeField]
    43.     private float _battery = 100f;
    44.     private float batteryDecreaseRate = 5f;
    45.     private float _maxBattery = 100f;
    46.     [SerializeField]
    47.     private bool _powered;
    48.     private SolarPanel _solarPanel;
    49.     private float _chargeRate;
    50.     private float _batteryCharge;
    51.     [SerializeField]
    52.     private bool _charging;
    53.  
    54.     private void Start()
    55.     {
    56.         _rb = GetComponent<Rigidbody>();
    57.         _rb.centerOfMass = _centerOfMass;
    58.         forward = false;
    59.         _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
    60.         _solarPanel = GameObject.Find("Solar Panel").GetComponent<SolarPanel>();
    61.         if (_uiManager == null)
    62.         {
    63.             Debug.LogError("The UI Manager is Null.");
    64.         }
    65.         if (_solarPanel == null)
    66.         {
    67.             Debug.LogError("The Solar Panel is Null.");
    68.         }
    69.     }
    70.  
    71.  
    72.     private void Update()
    73.     {
    74.         //AnimateWheels();
    75.         GetInputs();
    76.  
    77.         _chargeRate = _solarPanel.PowerProduct();
    78.  
    79.         if (_battery <= _maxBattery)
    80.         {
    81.             _batteryCharge += _chargeRate * Time.deltaTime;
    82.             _charging = true;
    83.  
    84.         }
    85.         else
    86.         {
    87.             _charging = false;
    88.         }
    89.        
    90.  
    91.         _uiManager.UpdateBattery(_battery);
    92.  
    93.         if (_battery <= 0)
    94.         {
    95.             _powered = false;
    96.         }
    97.         else
    98.         {
    99.             _powered = true;
    100.             if (_charging == false)
    101.             {
    102.                 _battery -= batteryDecreaseRate * Time.deltaTime;
    103.             }
    104.            
    105.         }
    106.     }
    107.  
    108.     private void LateUpdate()
    109.     {
    110.  
    111.         if (_powered == true)
    112.         {
    113.              Move();
    114.               //Turn();
    115.         }
    116.        
    117.        
    118.     }
    119.  
    120.     private void GetInputs()
    121.     {
    122.         inputX = Input.GetAxis("Horizontal");
    123.         inputY = Input.GetAxis("Vertical");
    124.     }
    125.  
    126.     private void Move()
    127.     {
    128.  
    129.         if (forward == true)
    130.         {
    131.             foreach (var wheel in wheels)
    132.             {
    133.                 wheel.collider.motorTorque = maxAcceleration * 500 * Time.deltaTime;
    134.             }
    135.         }
    136.     }
    137.  
    138.     //private void Turn()
    139.     //{
    140.  
    141.     //    if (forward == true) {
    142.     //    foreach (var wheel in wheels)
    143.     //    {
    144.     //        if (wheel.axel == Axel.Front)
    145.     //        {
    146.     //            var _steerAngle = inputX * turnSensitivity * maxSteerAngle;
    147.     //            wheel.collider.steerAngle = Mathf.Lerp(wheel.collider.steerAngle, _steerAngle, 0.5f);
    148.     //        }
    149.     //    }
    150.     //    }
    151.        
    152.     //}
    153.  
    154.     //private void AnimateWheels()
    155.     //{
    156.     //    foreach (var wheel in wheels)
    157.     //    {
    158.     //        Quaternion _rot;
    159.     //        Vector3 _pos;
    160.     //        wheel.collider.GetWorldPose(out _pos, out _rot);
    161.     //        wheel.model.transform.position = _pos;
    162.     //        wheel.model.transform.rotation = _rot;
    163.     //    }
    164.     //}
    165. }
    166.  
     
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    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.
     
    rainierchan12107 likes this.
  3. rainierchan12107

    rainierchan12107

    Joined:
    Jul 7, 2020
    Posts:
    1
    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.
     
  4. Grivik

    Grivik

    Joined:
    Apr 22, 2020
    Posts:
    5
    I got it working! thanks, guys. I was also overcomplicating it. It does that I'm looking for now.