Search Unity

The left-hand side of an assignment must be a variable, property or indexer

Discussion in 'Scripting' started by bmili, Jun 13, 2019.

  1. bmili

    bmili

    Joined:
    Jun 13, 2019
    Posts:
    3
    I can't seem to make the usageThisFrame to stop either when the player flashlight is off or when the BatteryPower is 0.
    Code (CSharp):
    1. public class Flashlight : MonoBehaviour
    2. {
    3.     private Light light;
    4.     float BatteryPowerMin = 0.0f;
    5.     float BatteryPower;
    6.     float FlashlightRate = 10.0f;
    7.     void Start()
    8.     {
    9.       light = GetComponent<Light>();
    10.         BatteryPower = 1000.0f;
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.  
    16.         float usageThisFrame = FlashlightRate * Time.deltaTime;
    17.  
    18.         BatteryPower -= usageThisFrame;
    19.  
    20.         Debug.Log("Charge remaining: " + BatteryPower);
    21.  
    22.         if (BatteryPower <= 0 || light.enabled = false)
    23.         {
    24.              usageThisFrame += BatteryPower;
    25.         }
    26.  
    27.         if (BatteryPower <= 0)
    28.         {
    29.             BatteryPower = BatteryPowerMin;
    30.         }
    31.        
    32.  
    33.  
    34.         if (Input.GetKeyUp (KeyCode.Q) & BatteryPower > BatteryPowerMin)
    35.         {
    36.             light.enabled = !light.enabled;
    37.         }
    38.     }
    39. }
    40.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If you want help with a compile error, always indicate which line is giving the error!

    But there's a lot of problems in the code you posted. Here's what I spot:
    • On line 22, you wrote = but you meant == (double equals means test if two things are equal, single equals means make them equal)
    • On line 34, you wrote & but you meant && (double ampersand is logical and, single ampersand is bitwise and)
    • I'm not sure what you think you're doing on line 24, but what you're actually doing is changing the value of a variable that will never be used again, i.e. absolutely nothing. I'm guessing you meant to increase BatteryPower by usageThisFrame, but what your code says is to increase usageThisFrame by BatteryPower. If your goal is to only reduce BatteryPower when the light is on and the battery is above zero, it would be smarter to put the subtraction inside an "if" rather than always subtracting and then trying to sometimes add it back.
    • Lines 27-30 probably won't do what you're expecting if BatteryPowerMin has any value other than 0.
    • You probably want something to turn the light off if the battery is empty. Right now you have something that prevents the light from being toggled, and something that looks like it's intended to stop the battery from draining into the negatives, but if the light is on and the battery is dead then the light stays on.
     
  3. bmili

    bmili

    Joined:
    Jun 13, 2019
    Posts:
    3
    already fixed it