Search Unity

Light Script - more than one lights

Discussion in 'Scripting' started by RoyS, Feb 9, 2019.

  1. RoyS

    RoyS

    Joined:
    Jan 12, 2009
    Posts:
    664
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnableLightTest : MonoBehaviour
    5. {
    6.     private Light myLight;
    7.  
    8.     void Start ()
    9.     {
    10.         myLight = GetComponent<Light>();
    11.     }
    12.  
    13.     void Update ()
    14.     {
    15.         if (Input.GetKeyDown (KeyCode.F))
    16.         {
    17.             myLight.enabled = !myLight.enabled;
    18.         }
    19.     }
    20. }
     
    Last edited: Feb 10, 2019
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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.
     
  3. RoyS

    RoyS

    Joined:
    Jan 12, 2009
    Posts:
    664
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnableLightTest : MonoBehaviour
    5. {
    6.     private Light myLight;
    7.     bool enter = false;
    8.  
    9.     void Start ()
    10.     {
    11.         myLight = GetComponent<Light>();
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         if (Input.GetKeyDown (KeyCode.F) && enter)
    17.         {
    18.             myLight.enabled = !myLight.enabled;
    19.         }
    20.     }
    21.  
    22.     void OnGUI()
    23.     {
    24.         if (enter)
    25.         {
    26.             GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 150, 30), "F for on/off");
    27.         }
    28.     }
    29.  
    30.     void OnTriggerEnter(Collider other)
    31.     {
    32.         if (other.CompareTag("Player"))
    33.         {
    34.             enter = true;
    35.         }
    36.     }
    37.  
    38.     void OnTriggerExit(Collider other)
    39.     {
    40.         if (other.CompareTag("Player"))
    41.         {
    42.             enter = false;
    43.         }
    44.     }
    45. }
     
    Last edited: Feb 10, 2019