Hi all,
I have a question, because I’m doing something wrong here.
I need to switch off light, when the player come into CollisionBox (Box Collider).
But after I added as component this script from below to my CollisionBox
and when player step into this box collider, nothing happen. Light still is on.
Can You help me somehow?
public class LightOff : MonoBehaviour
{
public Light myLight;
void Start()
{
GameObject light = GameObject.FindWithTag("TestLight");
myLight = light.GetComponent<Light>();
myLight.enabled = true;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
myLight.enabled = false;
}
}
So you need to debug it. What have you checked so far? This is the most important thing to mention beyond any script.
Have you checked that line 17 is called? Have you checked if OnTriggerEnter is called? This is stuff you need to do before posting on the forums because obviously nobody else can do that for you.
Maybe you’ve not set up the Layer Collision Matrix correctly or the Layer on the trigger isn’t what you want or the trigger GameObject doesn’t have a “Player” tag etc etc.
NOTE: You’ve tagged this post as “script errors”. So do you have script errors? You didn’t mention them. I suspect you’re tagging it with that because something isn’t doing what you expect? Also, tagging it with “performance”. Is there a performance related question here?
I recommend deleting this part of your code entirely:
GameObject light = GameObject.FindWithTag("TestLight");
myLight = light.GetComponent<Light>();
myLight is a public member, so all you need to do is drag the light into the inspector and you’ll have your reference. Never use any sort of “Find” method if you can get a reference in a simpler way.
The next step would be to put some Debug.Log statements in your OnTriggerEnter. THen you can see if the player is actually triggering the OnTriggerEnter and whether they are passing the tag comparison.