Turn on light with OnTriggerStay?

Hi guys,

I’m super new to Unity and to c# so bear with me (I’m probably talking nonsens). I’m using the OnTriggerStay on a trigger which is the white box, so if the player triggers the white box I would like the spot light shinging on the red box turn on. I have figured out a bit of the script but now I’m at a dead end so I really would appriciate a bit of help. Below is an image of my scene and the code I’m using.

Thanks in advance!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LightOff : MonoBehaviour {

    private Light myLight;

    public GameObject Light;


    void OnTriggerStay (Collider other)
    {
        Debug.Log ("entered");
        myLight.enabled = false;

    }
}

Well you haven’t stated what your problem is, but you are using Light as both the name of a variable and as a class on lines 7 and 9. Don’t do that. You also never set the reference to myLight before you try to use it. Thirdly, you are turning the light off on line 15 instead of turning it on. You probably want OnTriggerEnter and OnTriggerExit instead of OnTriggerStay by the way.

You might also would try using tags or something so you don’t run into problems later.
So inside your OnTriggerStay (as Joe said) make it an OnTriggerEnter/Exit instead.
But inside it, type

if(other.tag == “Player”){
Debug.Log (“entered”)ad
myLight.enabled = false;
}

The reason you do this, is so joe-blow enemy or falling wall or a bullet don’t set it off (Unless that’s what you want) of course.

Edit:
You can set a tag on your Player by clicking on the object on player that has a Collider and at the top of inspectors where it says tags, click it and select Player.

Also on this note, in the if statement above, change player to whatever you want, Player is just standard inside Unity, so I just put that.