So I found a great script to make the doors open and close on here, and I combined it with other scripts (learning from them the whole time) to make a simple apartment where I can open and close the fridge and freezer doors and open and close the bathroom door.
Feeling adventurous, I moved on to trying to turn the light on and off. Much scrabbling and whining later, I’ve got it to at least acknowledge that when I hover my reticle over the light and click the mouse button, it toggles to “off”.
Except instead of turning off, it’s flickering. I checked and it’s turning on and off until I click again, and I’m honestly not sure what I missed. So! Here’s my code:
public bool on = true;
public void ChangeLightState()
{
on = !on;
}
void Update () {
if(on) //on == true
{
GetComponent<Light>().enabled = GetComponent<Light>().enabled;
}
else
{
GetComponent<Light>().enabled = !GetComponent<Light>().enabled;
}
}
That’s the code I put on the light itself. My Interact Script is:
void Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, interactDistance))
if(hit.collider.CompareTag("Door"))
{
hit.collider.transform.parent.GetComponent<DoorScript>().ChangeDoorState();
}
if(hit.collider.CompareTag("Light"))
{
hit.collider.transform.GetComponent<ToggleLight>().ChangeLightState();
}
}
I’m missing something super obvious, right?
ETA: Also, is there a way to turn emissions off/on instead of light?