- Home /
FlashLight Fixes Please??
I have a working flashlight script that drains out the energy of my flashlight and turns it off, I want to fix some things, any suggestions will help a lot:):)
1) When the power of the flashlight goes out, i can still press f and hear the sound of the button to turn the flashlight on/of... How do i disable it until my flashlight has energy again??
2)I have another Script to pick up a flashlight object that i made by going through the object, the script goes attached to the player and the object must have a "FlashLight" tag. I would like that when i pick up this item, just then the first script gets activated, so then i can press "f" to turn the flashlight on and start draining it's batteries
3)I would prefer to pick the flashlight up by pressing "e" near it...:)
Thats all, If anyone can help me in any of the 3 problems it will mean a lot:)
The FlashlightPickUp Script(attached to the player):
 private var hit : RaycastHit;
 private var rayDistance : float;
 private var contactPoint : Vector3;
 var PickupSound: AudioClip;
     
 function OnTriggerEnter (other : Collider) {     
     if (other.CompareTag ("Flashlight")) { 
        audio.PlayOneShot(PickupSound);    
        Destroy(other.gameObject);               
     } 
 }
The FlashLight Script, wich drains energy and can be turned on/off with "f" key:
 var lightSource : Light; //Connect the light source in the Inspector
 var shootSound:AudioClip;
 static var energy : float = 100; //The energy amount of the flashlight
 static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
 private var drainSpeed : float = 9.0; //The speed that the energy is drained
  lightSource.enabled = false; 
 function Update () {
     if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
         
 }
  
 //When the player press F we toggle the flashlight on and off
 function ToggleFlashlight () {
     turnedOn=!turnedOn;
       audio.PlayOneShot(shootSound);
     if (turnedOn && energy>0) {
        TurnOnAndDrainEnergy();
     } else {
        lightSource.enabled = false;
      
     }
 }
  
 //When the flashlight is turned on we enter a while loop which drains the energy
 function TurnOnAndDrainEnergy () {
     lightSource.enabled = true;
     while (turnedOn && energy>0) {
        energy -= drainSpeed*Time.deltaTime;
        yield;
     }
     lightSource.enabled = false;
 //I tried here with----->   shootSound.enable = false;     ,it didn't work
 }
  
 //This is called from outside the script to alter the amount of energy
 static function AlterEnergy (amount : int) {
     energy = Mathf.Clamp(energy+amount, 0, 100);
 }
Answer by Crystalline · Oct 26, 2013 at 07:01 AM
If you want to make your flashlight work only if it has energy:
 function ToggleFlashlight () {
    if(energy>1){
        turnedOn=!turnedOn;
       audio.PlayOneShot(shootSound);
     if (turnedOn && energy>0) {
        TurnOnAndDrainEnergy();
     } else {
        lightSource.enabled = false;
     }
     }
 }
It did work!!!:), Any ideas of how to relate this script to the PickUp script so it only get's activated when i get the Flashlight object tagged "FlashLight"???
Your answer
 
 
             Follow this Question
Related Questions
How to pickup flashlight with "E" key ? 1 Answer
Very simple picking up items script? 2 Answers
Flashlight pickup, battery etc 3 Answers
Pick up item code is not working 2 Answers
Problems with key script 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                