- Home /
The question is answered, right answer was accepted
Lights on Script not always working
Hello, so I created a basic script that when "e" is pressed down the light specified turns on, then off again if you press it again. For some reason though the light doesn't always turn on when "e" is pressed and I have no idea why. Here's my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsOnPress : MonoBehaviour
{
public AudioSource Click;
public Light Lights;
public GameObject Text;
public bool LightisOn;
public void Start()
{
LightisOn = Lights.enabled = false;
}
void OnTriggerStay(Collider other)
{
Text.gameObject.SetActive(true);
if (Input.GetKeyDown("e"))
{
if (LightisOn)
{
LightisOn = Lights.enabled = false;
Click.Play();
}
else
{
LightisOn = Lights.enabled = true;
Click.Play();
}
}
}
void OnTriggerExit(Collider other)
{
Text.gameObject.SetActive(false);
}
}
Answer by yummy81 · Feb 22, 2018 at 11:29 AM
It's simple. Input event information should be processed in Update. Processing these events in FixedUpdate can lead to unpredictable results. When it comes to your code, the problem is that you check for the input inside OnTriggerStay, which acts as FixedUpdate. The solution would be to move this code into coroutine-turned OnTriggerEnter. So basically, it could look like that:
private IEnumerator OnTriggerEnter(Collider other)
{
while(true)
{
// here's your code from OnTriggerStay
yield return null;
}
}
As you can see, when coroutine version of OnTriggerEnter yields "null", it acts as Update. And, here's your script after modifications. It should now work as you want:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsOnPress : MonoBehaviour
{
public AudioSource Click;
public Light Lights;
public GameObject Text;
public bool LightisOn;
public void Start()
{
LightisOn = Lights.enabled = false;
}
IEnumerator OnTriggerEnter(Collider other)
{
Text.gameObject.SetActive(true);
while (true)
{
if (Input.GetKeyDown("e"))
{
if (LightisOn)
{
LightisOn = Lights.enabled = false;
Click.Play();
}
else
{
LightisOn = Lights.enabled = true;
Click.Play();
}
}
yield return null;
}
}
void OnTriggerExit(Collider other)
{
Text.gameObject.SetActive(false);
}
}
Hello,
thank you for the response. I have tried the script, it works for the most part but there's a few problems. When I enter the collider I am able to turn the light off but after exit I can turn it off an on whenever. Also no matter where I am "e" results in my "click" audio being played.
You're right! So, I introduced new Boolean and I named it "boo". I added or modified following lines: 11, 21, 23, and 44. Here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsOnPress : $$anonymous$$onoBehaviour
{
public AudioSource Click;
public Light Lights;
public GameObject Text;
public bool LightisOn;
public bool boo;
public void Start()
{
LightisOn = Lights.enabled = false;
}
IEnumerator OnTriggerEnter(Collider other)
{
Text.gameObject.SetActive(true);
boo = true;
while (boo)
{
if (Input.Get$$anonymous$$eyDown("e"))
{
if (LightisOn)
{
LightisOn = Lights.enabled = false;
Click.Play();
}
else
{
LightisOn = Lights.enabled = true;
Click.Play();
}
}
yield return null;
}
}
void OnTriggerExit(Collider other)
{
boo = false;
Text.gameObject.SetActive(false);
}
}
Thank you very much, it worked! I have accepted your answer as the correct one. I hope you have a good day! :)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dissable lights per camera? 2 Answers
Enable Draw Halo in Light Component via Script 1 Answer
I can't figure out why this if statement isn't responding 1 Answer