- Home /
How to make a flashlight toggleable in C#?
I haven't been able to find any working scripts/tutorials on this. I just need to know how to turn off a flashlight with the f key.
Answer by Dibbie · Jun 01, 2015 at 01:37 AM
You could use a input function. Im just assuming your "flashlight" is actually a directional light in Unity.
JAVASCRIPT:
var flashlight : Light;
function Update () {
if(input.GetKeyDown(KeyCode.F) && flashlight.enabled == false){
flashlight.enabled = true;
}else if(input.GetKeyDown(KeyCode.F) && flashlight.enabled == true){
flashlight.enabled = false;
}
}
C#:
public Light flashlight;
void Update () {
if(input.GetKeyDown(KeyCode.F) && flashlight.enabled == false){
flashlight.enabled = true;
}else if(input.GetKeyDown(KeyCode.F) && flashlight.enabled == true){
flashlight.enabled = false;
}
}
That should work for you.
Answer by Chasing_Wyatt · Jun 10, 2016 at 04:33 PM
Here's a flashlight script I use:
using UnityEngine; using System.Collections;
public class Flashlight : MonoBehaviour {
//Flashlight Object
public Light flashlight;
//Flashlight power variables
public float power = 100.0f;
private float maxPower = 100.0f;
private float minPower = 0.0f;
//Battery charge variable
private float batteryCharge = 100.0f;
//Number of batteries currently possesed by the player
public int batteryCount = 3;
//Power Drain controls how fast the battery life decreases
public float powerDrain = 1.0f;
//Boolean that tells whether or not the flashlight is able to be used based off of current power
private bool usable = true;
void Start ()
{
}
void Update ()
{
//If the F key is pressed and the power is greater than zero, then the flashlight will toggle between on and off
if (Input.GetKeyDown (KeyCode.F) && usable)
{
flashlight.enabled = !flashlight.enabled;
}
//While the flashlight is off, the power will drain
if (flashlight.enabled)
{
power -= Time.deltaTime * powerDrain;
}
//This is to ensure that the power will never go over 100
if (power > maxPower)
{
power = maxPower;
}
//This is to disable to flashlight and make sure it can't be used until the player uses a battery to recharge the flashlight
if (power < minPower)
{
power = minPower;
flashlight.enabled = false;
usable = false;
}
//After you replace the batteries, it allows you to use the flashlight again
if (power > minPower)
{
usable = true;
}
//This says that if the player has at least one battery, and if they press R, then the flashlight will be fully charged
if (Input.GetKeyDown (KeyCode.R) && batteryCount > 0)
{
power += batteryCharge;
batteryCount -= 1;
}
}
}
And here's the battery script I use:
using UnityEngine; using System.Collections;
public class Battery : MonoBehaviour {
//Battery game object
public GameObject battery;
//Boolean that tells if the player is in the battery's collider
private bool trig = false;
//When player enters the collider, player will be able to collect the battery
void OnTriggerEnter ()
{
trig = true;
}
//When player exits the collider, player will be not able to collect the battery
void OnTriggerExit ()
{
trig = false;
}
void Start ()
{
trig = false;
}
void Update ()
{
//When the player enters the trigger and presses the E key, they will recieve a new battery and the battery they collected will be removed from the scene
if (trig && Input.GetKeyDown (KeyCode.E))
{
//Finds and acesses the flashlight script
Flashlight flashlight = GameObject.FindGameObjectWithTag("Flashlight").gameObject.GetComponent<Flashlight> ();
//Adds a battery
flashlight.batteryCount += 1;
//Removes the battery collected from the scene
Destroy (battery);
}
}
}
An important thing to note: You'll need to tag your flashlight as "Flashlight" so that the battery script can access the batteryCount variable.
Answer by chesterhilly · Jul 24, 2016 at 02:21 PM
@blackuethel Here is a simple way to turn a light on and off by Clicking
using UnityEngine;
using System.Collections;
public class Flashlight : MonoBehaviour {
public Light light; //assign gameobject with light component attached
void Update () {
if (Input.GetMouseButtonDown (0)) { //Left mouse button
light.enabled = !light.enabled; //changes light on/off
}
}
}
Make sure you add the script to GameObject with the light component.
Then assign the same GameObject to 'light'.
Answer by hananaja132 · Oct 18, 2017 at 05:39 AM
how to make toogleable flashlight button in unity,can someone give me the script please...
Answer by JohnnyRandom1212 · Nov 04, 2018 at 07:51 PM
um hi why does my batterycount keep on dropping below 0 and nonstop i followed ure code as is, can some one help plz
Here is mycode using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Holo_Cube : $$anonymous$$onoBehaviour { //Flashlight Object public Light holoLight;
//Flashlight Power variables
public float holoBattery = 100.0f;
private float maxPower = 100.0f;
private float $$anonymous$$Power = 0.0f;
//Battery Charge variable
private float batteryCharge = 100.0f;
//Numberof batteries currently possesed by the player
public int batteryCount = 3;
//Power Drain controls how fast the battery life decreases
public float powerDrain = 1.0f;
//Boolean that tells whether or not the flashlight is able to be used based off of current power
private bool usable = true;
void Update()
{
//If the F key is pressed and the power is greater than zero, then the flashlight will toggle between onand off
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.F) && usable)
{
holoLight.enabled = !holoLight.enabled;
}
//While the Flashlight is on, the power will drain
if (holoLight.enabled)
{
holoBattery -= Time.deltaTime * powerDrain;
}
//THis is to ensure that the power will never go over 100
if (holoBattery > maxPower)
{
holoBattery = maxPower;
}
//This is to disable the flashlight and make sure it cant be used until the player uses a battery to recharge the flashlight
if (powerDrain < $$anonymous$$Power)
{
holoBattery = $$anonymous$$Power;
holoLight.enabled = false;
usable = false;
}
//After you replace the batteries, it allows you to usethe flashlight again
if (holoBattery > $$anonymous$$Power)
{
holoBattery += batteryCharge;
batteryCount -= 1;
}
//Checks for batteries below 0
//if (batteryCount > 0)
//{
// batteryCount = 0;
//}
}
}