Created
February 8, 2017 23:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} | |
} |
hello im new so can you explain how this scripts work i need to create a spot light or something?
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.
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
.
Thanks this saved my project
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what if i want to toggle it on trigger?