Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created February 12, 2022 19:38
Show Gist options
  • Save kurtdekker/cfdf56b5c6cd45619b1a3e278560384e to your computer and use it in GitHub Desktop.
Save kurtdekker/cfdf56b5c6cd45619b1a3e278560384e to your computer and use it in GitHub Desktop.
Simple flashlight script
using UnityEngine;
using System.Collections;
// @kurtdekker
// place this on the flashlight, which must have a Light component
public class Flashlight : MonoBehaviour
{
public float battery = 20;
bool isOn;
void Update()
{
if (Input.GetKeyDown("f"))
{
isOn = !isOn;
}
if (isOn)
{
battery = battery - Time.deltaTime;
}
if (battery <= 0)
{
isOn = false;
}
GetComponent<Light>().enabled = isOn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment