Search Unity

Flashlight batteries problem

Discussion in 'Scripting' started by Dari, Nov 2, 2013.

  1. Dari

    Dari

    Joined:
    Mar 25, 2013
    Posts:
    130
    I have the script for the light and batteries won't work.

    Script:

    Code (csharp):
    1. var maxBatteryLife: float = 100;
    2.     var batteryLifeLostPerSecond: float = 1;
    3.     var linkedLight: Light;
    4.     var intensityLost: float = 0.01;
    5.     var currentBatteryLife: float;
    6.     var showHelpAfter : float = 15;
    7.    
    8.     private var gui : boolean = false;
    9.    
    10.     function Start() {
    11.     currentBatteryLife = maxBatteryLife;
    12.     }
    13.      
    14.     function Update() {
    15.     if(Input.GetKeyDown("f")) {
    16.     linkedLight.enabled = !linkedLight.enabled;
    17.     }
    18.     if(linkedLight.enabled) {
    19.     currentBatteryLife -= batteryLifeLostPerSecond * Time.deltaTime;
    20.     linkedLight.light.intensity -= intensityLost * Time.deltaTime;
    21.     }
    22.     if(currentBatteryLife <= 0) {
    23.     linkedLight.enabled = false;
    24.     }
    25.     if(currentBatteryLife <= showHelpAfter) {
    26.     gui = true;
    27.     }
    28.     }
    29.    
    30.     function OnGUI() {
    31.     if(gui) {
    32.     GUI.Box(new Rect(Screen.width/2 - 165, Screen.height - 100, 350, 25), "Press (R) button to reload batteries");
    33.     }
    34.     }
    35.    
    36.     function OnTriggerEnter(other : Collider) {
    37.     if(other.gameObject.tag == "Battery") {
    38.     currentBatteryLife = maxBatteryLife;
    39.     }
    40.     }  
    I've put the script on trigger collider and when the object with tag "Battery" enters the trigger, nothing happens, please help! (battery object is not trigger)
    Thanks in advance! :)
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    I think you should put currentBatteryLife = maxBatteryLife; under function Update instead of Start and change the CurrentBatteryLife variable to 100 cuz i believe that's how you want it when the game starts.

    + Add a boolean called HasBattery and make it true only when you enter the trigger. Under function Update, add an "If" Statement and make CurrentBatteryLife = MaxBatteryLife when HasBattery = true.
     
    Last edited: Nov 2, 2013
  3. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    You can't use the OnTriggerEnter on the object that is controlling the flashlight logic. You need to put that in a separate class/file that includes that function. Reverse the logic however. So when a player hits the battery it will "Pick it up" and set your battery. So each of the BatteryPickUp Objects will include the bottom script as well as have a rigid body attach and have a trigger collider.

    Code (csharp):
    1.  
    2.     var maxBatteryLife: float = 100;
    3.         var batteryLifeLostPerSecond: float = 1;
    4.         var linkedLight: Light;
    5.         var intensityLost: float = 0.01;
    6.         var currentBatteryLife: float;
    7.         var showHelpAfter : float = 15;
    8.        
    9.         private var gui : boolean = false;
    10.        
    11.         function Start() {
    12.         currentBatteryLife = maxBatteryLife;
    13.         }
    14.          
    15.         function Update() {
    16.         if(Input.GetKeyDown("f")) {
    17.         linkedLight.enabled = !linkedLight.enabled;
    18.         }
    19.         if(linkedLight.enabled) {
    20.         currentBatteryLife -= batteryLifeLostPerSecond * Time.deltaTime;
    21.         linkedLight.light.intensity -= intensityLost * Time.deltaTime;
    22.         }
    23.         if(currentBatteryLife <= 0) {
    24.         linkedLight.enabled = false;
    25.         }
    26.         if(currentBatteryLife <= showHelpAfter) {
    27.         gui = true;
    28.         }
    29.         }
    30.        
    31.         function OnGUI() {
    32.         if(gui) {
    33.         GUI.Box(new Rect(Screen.width/2 - 165, Screen.height - 100, 350, 25), "Press (R) button to reload batteries");
    34.         }
    35.         }
    36.        
    37.        public function SetBatteryValue(float amount) {
    38.               if(amount > maxBatteryLife) {
    39.                        currentBatteryLife = maxBatteryLife;
    40.               } else {
    41.                        currentBatteryLife = amount;
    42.               }
    43.        }
    44.      
    45.  
    Code (csharp):
    1.  
    2.  
    3.   var myBatteryValue = 100;
    4.   function OnTriggerEnter(other : Collider) {
    5.        if(other.collider.gameObject.GetComponent("FlashLightClassName") != null) {
    6.              FlashLightClass flashLight = other.collider.gameObject.GetComponent("FlashLightClassName");
    7.              flashLight.SetBatteryValue(myBatteryValue);
    8.        }
    9.    }
    10.  
     
  4. Dari

    Dari

    Joined:
    Mar 25, 2013
    Posts:
    130
    Ummm, errors, I don't know how to fix them.

    Assets/Battery.js(4,33): UCE0001: ';' expected. Insert a semicolon at the end.
    I don't know for this one.

    Assets/Doors/FlashlightBattery.js(36,50): BCE0043: Unexpected token: amount.
    Am I supposed to add a new variable called "amount"?
     
    Last edited: Nov 2, 2013