I want to turn on light my while im staying my box collider

I want to turn on light my while im staying in my box trigger and if i press ‘e’ it turn off lights (sorry for my bad english)

public GameObject MyLight;
//public Light MyLight;

void OnTriggerStay(Collider other)
{
    if(other.CompareTag("Player"))
    {
        if(Input.GetKeyDown(KeyCode.E))
        {
            MyLight.SetActive(true);
            // if(!MyLight.enabled){
            //       MyLight.enabled = true;
            // }
            // else if(MyLight.enabled){
            //        MyLight.enabled = false;
            // }
        }
    }
}

That will turn it on. Turning it off is just as easy. Also, you could use Light instead of gameObject. Theres many ways to do this.

1 Like

so

  1. By default the light is off. When you enter the trigger box the light turns on.
  2. When you leave the trigger box the light turns off(?)
  3. However, pressing “e” inside the trigger box turns the light off

There’s some ambiguous stuff (does pressing “e” again turn it back on? etc)

Assuming you have the box collider attached to the light object itself, you would do something like this.

void OnTriggerEnter(Collider other) {

    Debug.Log("Player entered trigger box");

    // enable the light component of this GameObject
    gameObject.GetComponent<Light>().enabled = true;

    // if player presses "e" turn off the light
    if (Input.GetKeyDown(KeyCode.E) {
        gameObject.GetComponent<Light>().enabled = false;
  
    }

}

void OnTriggerExit(Collider other) {

    Debug.Log("Player left the trigger box");

    // disable the light component of this GameObject
    gameObject.GetComponent<Light>().enabled = false;

}
1 Like

You should set a bool to true when the object is inside the box collider. Also, turn the light on at this time.

During update, check if the bool is true, plus if the key was pressed to turn off the lights or toggle them.
Set the bool back to false upon exit from the collider.

TriggerStay could miss a GetKeyDown event, and TriggerEnter will almost certainly miss it.

Sorry, that is not correct… not the part about lousy performance to check a bool nor the part about it not missing a keyDown event.

Update is completely reliable for a Down/Up key event.

As for why it’s almost always missed in TriggerEnter – that method is only called on the first physics update that it occurs. Likewise, KeyDown is only on the 1st frame it occurs. You can just imagine from there.

Void OnTriggerEnter() {

    if (Input.GetKeyDown(Keycode.W) {
        Debug.Log("W key pressed")
    }

}

That will output the debug message everytime the W key is pressed when the player is inside the trigger, not just on the first frame. I already mentioned that changing the rigidbody on the player to constant update detection resolves the issue of OnTriggerEnter not being called.

Adding unnecessary code to the Update method (when you can have it outside of update) is certainly a drag on performance.

https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html

“Use this to set up a Rigidbody’s for continuous collision detection, which is used to prevent fast moving objects from passing through other objects without detecting collisions.”

The OP can test the options mentioned in the thread and see what works best. :slight_smile:

1 Like

True. I guess it depends on what they’re trying to achieve. The method I mentioned might not be the best if they don’t have a ton of stuff to optimise (like constantly checking if 50+ lights are turned on/off via Update) as changing the collision detection to constant or dynamic can have it’s own impact on performance.

im just wanting if i in trigger and i press e turn on light and if i press e again it should does turn off light while in trigger (like a bulb)

Did you try the suggestion in my first post?

i tried something but i didnt :confused:

Can you post the code that you tried, using code tags: Using code tags properly - Unity Engine - Unity Discussions

I think I know what you are asking here, and you should just need to add an if statement to your script.

IF you are asking to ONLY by able to turn the light on and off when near the “switch” trigger,
then try setting up your trigger like this

[HideInInspector] bool lightOn;

void onTriggerEnter(Collider other)
{

        if(other.gameobject.CompareTag("Your Tag Name" && Input.GetKeyDown(KeyCode.C))
     {
            if (lightOn == false)
           {
                   gameObject.GetComponent<Light>().enabled = true;
                   lightOn = true;
           }
            else
           {
                 gameObject.GetComponent<Light>().enabled = false;
                 lightOn = false;
           }
    }

}



}

side note, if this doesn’t help, ALWAYS post the code you currently have when asking a question on here. the people here are super helpful and will actually show you how to do everything perfectly.

it is giving error "you can’t use this ‘&&’ with string or bool "

Hey there… have you gone through some tutorials here? Learn

That script was missing a closing bracket for the CompareTag, OnTriggerEnter wasn’t spelled correctly, and GetKeyDown won’t be reliable in that method, as I tried to explain.

okay then just layer the if statements then Im sure there is a more efficient way to do it but I don’t know it

[HideInInspector] bool lightOn;
void onTriggerEnter(Collider other)
{
        if(other.gameobject.CompareTag("Your Tag Name" )
     {
           if(Input.GetKeyDown(KeyCode.C))     //again there's probably a better way to do this
              {
                if (lightOn == false)
                 {
                        gameObject.GetComponent<Light>().enabled = true;
                         lightOn = true;
                  }
                 else
                 {
                        gameObject.GetComponent<Light>().enabled = false;
                         lightOn = false;
                 }
              }
     }
}

Oh i also see I forgot a ) in that fist if statment
i wrote
if(other.gameobject.CompareTag(“Your Tag Name” && Input.GetKeyDown(KeyCode.C))

it should be----------------------------------------------------------/
if(other.gameobject.CompareTag(“Your Tag Name”) && Input.GetKeyDown(KeyCode.C))

im assuming you just copy/pasted so try fixing that and see it that works

i made, and it is working thans for help.
now i want to show a hand on screen while i m staying in trigger i did someting but it didn’t work

public Light light;
    public bool open;

    void Start()
    {

    }
    void OnTriggerStay()
    {
        if (Input.GetKeyDown (KeyCode.F)) {
            light = !light;
        }
        if (open) {
            light.enabled = true;
       
   
        } else {
            light.enabled = false;

        }

    }

hand picture comes when i enter and stay in trigger but when i leave trigger it doesn’t leave

public Light light;
    public bool open;
    public     RawImage hand;
    void Start()
    {
        hand.enabled = false;
    }
    void OnTriggerStay()
    {
        if (Input.GetKeyDown (KeyCode.F)) {
            light = !light;

        }
        if (open) {
            light.enabled = true;
            hand.enabled = true;
   
        } else {
            light.enabled = false;
            hand.enabled = false;

        }

    }
}
1 Like

Where/how is ‘open’ set?

i just translated it, its working i just wondering image :confused:

I didn’t understand your reply. Can ‘open’ change while you’re still inside the trigger? The code below may require some modification if so…

However, here’s just a quick write up…

bool insideTrigger = false;
void OnTriggerEnter() {
 // there is no code here to ensure it's just the player, just noting that.
 insideTrigger = true;
 hand.enabled = true;
  }
void OnTriggerExit() {
  insideTrigger = false;
  hand.enabled = false;
 }
void Update() {
  if(insideTrigger && Input.GetKeyDown (KeyCode.F))
     light.enabled = !light.enabled;
 }
2 Likes