- Home /
How to set flashlight to off...
Hello Everyone my game is a cross between a horror survival game and a rpg/exploration game. The player is equipped with a flashlight from the very start of the game and has to find batteries to power the flashlight, using some tutorials and some reference scripts I have all that working the problem I have encountered is that the flashlight is on when the level starts. How would I go about setting the flashlight to be off when the level starts? Here is the script
//Name this script Flashlight and attach it to your player for instance
var switchsound : AudioClip;
var lightSource : Light; //Connect the light source in the Inspector
static var energy : float = 0; //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 = 2.0; //The speed that the energy is drained
function Update () {
if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
}
//When the player press F we toggle the flashlight on and off
function ToggleFlashlight () {
audio.PlayOneShot(switchsound);
turnedOn=!turnedOn;
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;
}
//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);
}
function OnGUI () {
GUI.Label (Rect (10, 10, 150, 20), Flashlight.energy.ToString());
}
any guidance would be greatly appreciated. Muchas Gracias
Answer by roojerry · Aug 08, 2013 at 08:30 PM
How bout just disabling the light on Start
function Start(){
lightSource.enabled = false;
}
or un-ticking the toggle on the light component attached to the gameobject
@brianruggieri Thank you that worked I had tried that but it did not work before maybe I was not putting it in the right place but it works now muchas gracias.
@brianruggieri The problem I am having now is that it works when I'm working in the editor but when I build and run it the flashlight does not turn on at all.