Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Created November 25, 2013 21:29
Show Gist options
  • Save jakevsrobots/7649216 to your computer and use it in GitHub Desktop.
Save jakevsrobots/7649216 to your computer and use it in GitHub Desktop.
Script to toggle a flashlight on and off. Attach this component to an object with a spotlight component. Add an AudioSource so the switch sound will play. Also remember to adjust the ambient color of the scene for darkness: (Edit->Render Settings->Ambient color).
using UnityEngine;
using System.Collections;
public class Flashlight : MonoBehaviour {
public AudioClip switchSound;
void Update () {
if(Input.GetKeyDown(KeyCode.F))
{
audio.PlayOneShot(switchSound);
if(light.enabled)
{
light.enabled = false;
}
else
{
light.enabled = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment