I have several lamps. I want to turn on one lamp and have the others stay off until I turn them on. This will turn on and off all lamps. This script is attached to the light itself. I've searched the forums, google and youtube and see scripts for one light, but not many that will act independent of each other. Code (CSharp): using UnityEngine; using System.Collections; public class EnableLightTest : MonoBehaviour { private Light myLight; void Start () { myLight = GetComponent<Light>(); } void Update () { if (Input.GetKeyDown (KeyCode.F)) { myLight.enabled = !myLight.enabled; } } }
Line 15 responds to the F key. That means every light that this script is on responds to the F key. If you want, you can put that KeyCode.F into a public inspector variable and compare it to that variable. That will let you have each light controlled by a separate key. If you want to base it on the closest light to your player, you need to get all the lights (by finding or by specifying in advance), iterate them to find the closest one, and operate only on that one. If you want to be even more complicated you can use trigger colliders and rigidbodies to define where the player has to be in order to operate each light.
So put a trigger collider on each lamp (I did for the above script, but I see what you're saying) and the script should be... Code (CSharp): using UnityEngine; using System.Collections; public class EnableLightTest : MonoBehaviour { private Light myLight; bool enter = false; void Start () { myLight = GetComponent<Light>(); } void Update () { if (Input.GetKeyDown (KeyCode.F) && enter) { myLight.enabled = !myLight.enabled; } } void OnGUI() { if (enter) { GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 150, 30), "F for on/off"); } } void OnTriggerEnter(Collider other) { if (other.CompareTag("Player")) { enter = true; } } void OnTriggerExit(Collider other) { if (other.CompareTag("Player")) { enter = false; } } }