Skip to content

Instantly share code, notes, and snippets.

@donovankeith
Created February 8, 2017 23:31
Show Gist options
  • Save donovankeith/a8688996d942ee0391f98e4904370c54 to your computer and use it in GitHub Desktop.
Save donovankeith/a8688996d942ee0391f98e4904370c54 to your computer and use it in GitHub Desktop.
C# Unity Script: Toggles a light on/off when user presses the `L` key.
// ToggleLight.cs
// Turns the light component of this object on/off when the user presses and releases the `L` key.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToggleLight : MonoBehaviour {
Light light;
// Use this for initialization
void Start () {
light = GetComponent<Light> ();
}
// Update is called once per frame
void Update () {
// Toggle light on/off when L key is pressed.
if (Input.GetKeyUp (KeyCode.L)) {
light.enabled = !light.enabled;
}
}
}
@Whisleman
Copy link

what if i want to toggle it on trigger?

@phielfy
Copy link

phielfy commented Feb 9, 2021

hello im new so can you explain how this scripts work i need to create a spot light or something?

@donovankeith
Copy link
Author

hello im new so can you explain how this scripts work i need to create a spot light or something?

Yes, add this script to any Light object.

@donovankeith
Copy link
Author

what if i want to toggle it on trigger?

You'd probably want to do light.enabled = !light.enabled; not in Update() but in void OnTriggerEnter.

@mrttt123456
Copy link

Thanks this saved my project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment