Search Unity

Rechargeable Battery Setup

Discussion in 'Scripting' started by Control-definition-launch-file, Jun 13, 2019.

  1. Control-definition-launch-file

    Control-definition-launch-file

    Joined:
    Jun 12, 2019
    Posts:
    7
    So, I am writing this code (it's for the degrading battery setup) and I am having trouble with it. Basically I am wanting to create batteries that can be picked up and used to recharge the power meter.

    Code (CSharp):
    1. public class BatteryLife : MonoBehaviour
    2. {
    3.     float BatteryPowerMax = 1000.0f;
    4.     float BatteryPowerMin = 0.0f;
    5.     public float BatteryPower;
    6.     float FlashlightRate = 10.0f;
    7.     private Light playerLight;
    8.     void Start()
    9.     {
    10.       playerLight = GetComponent<Light>();
    11.         BatteryPower = 1000.0f;
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         float usageThisFrame = FlashlightRate * Time.deltaTime;
    17.  
    18.         BatteryPower -= usageThisFrame;
    19.  
    20.         Debug.Log("Charge remaining: " + BatteryPower);
    21.  
    22.         if (BatteryPower <= 0)
    23.         {
    24.             playerLight.enabled = false;
    25.         }
    26.         else
    27.         {
    28.             playerLight.enabled = true;
    29.         }
    30.         if (BatteryPower <= BatteryPowerMin)
    31.         {
    32.             BatteryPower = 0f;
    33.         }
    34.     }
    35.    
    36. }
    37.  
    There isn't really anything wrong here, but I can't get it to communicate with the Recharge script.
    Code (CSharp):
    1. public class Recharge : Item
    2. {
    3.     public int charge = 0;
    4.  
    5.     public override void Use()
    6.     {
    7.         GameObject player = Inventory.instance.player;
    8.         BatteryLife2 batteryLife2 = player.GetComponent<BatteryPower>();
    9.         charge += BatteryPower
    10.     }
    11. }
    It's supposed to add a charge based on what you picked up. (Ex. small battery)
     
  2. Control-definition-launch-file

    Control-definition-launch-file

    Joined:
    Jun 12, 2019
    Posts:
    7
    Nvm, I figured it out.
     
    EdGunther and Homicide like this.
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    Here's to a generation of people's Phone Batteries gettin "de-rained"... lol. j/k