using System.Collections; using System.Collections.Generic; using UnityEngine; public class LightSwitch : MonoBehaviour { public bool lightIsOn; // You can change from the inspector if you want the light to be on or off as the default. [SerializeField] private GameObject _lightΒulbs; private Light[] _light; private Renderer[] _lightRend; private SanityActivation[] sanity; private static LightSwitch _instance; public static LightSwitch GetInstance() { return _instance; } void Awake() { if (!_instance) { _instance = this; } _light = _lightΒulbs.GetComponentsInChildren(); _lightRend = _lightΒulbs.GetComponentsInChildren(); sanity = _lightΒulbs.GetComponentsInChildren(); } private void Start() { //Sets the default of the lights if (lightIsOn) { foreach (Light light in _light) { light.enabled = true; } foreach (Renderer renderer in _lightRend) { renderer.material.EnableKeyword("_EMISSION"); } foreach (SanityActivation sanity in sanity) { sanity.EnableSanityTrigger(); } } else if (!lightIsOn) { //_light.enabled = false; foreach (Light light in _light) { light.enabled = false; } foreach (Renderer renderer in _lightRend) { renderer.material.DisableKeyword("_EMISSION"); } foreach (SanityActivation sanity in sanity) { sanity.DisableSanityTrigger(); } } } public void lightSwitch() { if(!lightIsOn) //If light is not on & you press the following input: { if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton0)/*A button*/) { lightIsOn = true; foreach (Light light in _light) { light.enabled = true; } //_light.enabled = true; foreach (Renderer renderer in _lightRend) { renderer.material.EnableKeyword("_EMISSION"); } foreach (SanityActivation sanity in sanity) { sanity.EnableSanityTrigger(); } print("Light is on"); } } else if (lightIsOn && Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton0)/*A button*/) //if light is on & you press the following input. { lightIsOn = false; foreach (Light light in _light) { light.enabled = false; } //_light.enabled = false; foreach (Renderer renderer in _lightRend) { renderer.material.DisableKeyword("_EMISSION"); } foreach (SanityActivation sanity in sanity) { sanity.DisableSanityTrigger(); } print("Light is off"); } } }