How do i get it that only "1" flashlight turns on and off?

Hello,

I am currently working on a script that has my character pick up a flashlightItem, and while it is equipped, he should be able to turn it on and off (disable lights), but for some reason, it turns on/off the flashlights in the entire scene.
I have a similar script for fires that work just fine (uses particles AND lights), but for some reason it wont work for my flashlight.

Would someone please be kind enough to look at my script and see what im missing?
thank you, it is very much appreciated.
This is my script

#pragma strict

private var FlashightIsOn = false;

private var FlashlightIsTurnedOn = false;

//for lightBeam turning on, etc
var FlashlightBeam : Light; 		 //light produced from beam

//For lightHalo turning on, etc
var FlashLightHalo : Light;		// Halo produced from light


function Awake ()
{
FlashlightBeam.enabled = false;  // turns off the light beam

FlashLightHalo.enabled = false;  // turns off the light halo

}

function Update () 
	{

if (FlashlightIsTurnedOn == false && Input.GetButtonDown("Fire1"))
		{   
		 TurnFlashlightON ();

		}
	else if (FlashlightIsTurnedOn == true && Input.GetKeyDown(KeyCode.E))
		{
		TurnFlashlightOFF ();
		
		}
	}

function TurnFlashlightON ()
	{
		FlashlightIsTurnedOn = true;
		FlashlightBeam.enabled = true;  // turns off the light beam
		FlashLightHalo.enabled = true;  // turns off the light halo
	}

function TurnFlashlightOFF ()
	{
		FlashlightIsTurnedOn = false;
		FlashlightBeam.enabled = false;  // turns off the light beam
		FlashLightHalo.enabled = false;  // turns off the light halo
	}

1 Answer

1

If this script is on every flashlight in the scene, it will affect every flashlight in the scene.
I’m assuming that’s what you did, and there is your problem.

You need just one of this script attached to the player, or better yet a faceless GameObject that handles all input.

The collectable flashlights should not be the same object as the equipped flashlight. They just need trigger colliders on them that make them disappear set the players “hasFlashlight” boolean to true and enable the flashlight in their hand.

the way it works is that flashlights can be picked up in the game via inventory. once it is equipped, you can turn it on or off. so there is only one flashlight with the script starting the game, but players could find more. also, I have a similar script with my firepits, and they work fine, although it doesn't seem to work with the flashlights...

you can use (like Kiloblargh said) a boolean to check if the flashlight is being held or active or whatever, it should work. like, if the flashlight is in the inventory, set "isActive" or "hasFlashlight" or something else to true, otherwise it would be false, and when it's false (do something)

@Therian: I don't think you got my point. Every script in the scene that has an Update() function will call that function every frame. So they all detect the key press. If you want to still do it your way, make another boolean "held" and in Update(), just put if (!held) {return;}. Then when you pick up the flashlight, set it true. Alternately, you could put this.enabled = false; in Awake(), and enable the script when you equip the flashlight.

There's nothing technically wrong with it, but you should always make variables start with a lowercase letter, and you shouldn't have one function called "TurnOnLight()" and another called "TurnLightON()", because you're going to confuse yourself. Also, there is no reason to have a function whose only purpose is to set a boolean true. You can directly set a variable in one script from any other script using dot notation, the same way you can call a function on another script.

Thank you. I just noticed the two similar functions I had. I went ahead and used the dot.notation function. I didn't know you could change a variable that way. I thought you had to always call a function. so you taught me something useful today. :) thank you. But now it is working fine. Thank you very much for your help. :D