codfel4

Lamp Code

Dec 2nd, 2020
648
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode]
  4. public class LampScript : MonoBehaviour
  5. {
  6.     public GameObject lampContainer;
  7.  
  8.     public GameObject haloLight;
  9.     public GameObject emittedLight;
  10.    
  11.     public GameObject lampOn;
  12.     public GameObject lampOff;
  13.    
  14.     public bool isLampOn;
  15.     private bool _currentLampState = false;
  16.    
  17.     void Start()
  18.     {
  19.         if(LampChanged()) ChangeLamp();
  20.     }
  21.     bool LampChanged()
  22.     {
  23.         return isLampOn != _currentLampState;
  24.     }
  25.     void ChangeLamp()
  26.     {
  27.         haloLight.GetComponent<Light>().enabled = !haloLight.GetComponent<Light>().enabled;
  28.         emittedLight.GetComponent<Light>().enabled = !emittedLight.GetComponent<Light>().enabled;
  29.  
  30.         Instantiate(isLampOn ? lampOn : lampOff, lampContainer.transform);
  31.  
  32.         _currentLampState = !_currentLampState;
  33.     }
  34.  
  35.     void Update() {
  36.         if(LampChanged()) ChangeLamp();
  37.     }
  38. }
RAW Paste Data