Files
2025-05-29 22:31:40 +03:00

111 lines
3.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Light>();
_lightRend = _lightΒulbs.GetComponentsInChildren<Renderer>();
sanity = _lightΒulbs.GetComponentsInChildren<SanityActivation>();
}
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");
}
}
}