LIght on/off

Hi everyone, i have to turn a light on and off from another gameobject, essentially when i enter a collider and press a key light turn on or off.
i am very noob at scripting but i tried using some “booleans” and “if condition” failing…

I tried to make another script to only turn on the light with all the API reference and manuals but it won’t work.
Please help me.

public var light : GameObject;

function Start(){
 light.SetActive == false;
 
function OnTriggerEnter(Other : Collider){

     if(Input.GetKeyDown ("Mouse0")){

        light.SetActive == true;
     }
 }`

Well at the moment essentially everything past the public var is wrong. I’m feeling generous though…

if (Input.GetMouseButtonDown(0))
{
    light.SetActive(true);
}

That is what you are looking for. However that will disable the entire GameObject which has the light on it so any scripts on it will also stop working.

if (Input.GetMouseButtonDown(0))
{
   light.GetComponent<Light>().enabled = false;
}

That will just switch the light off and keep the GameObject active. That’s CS not JS though so the syntax may be different in JavaScript.

Now to the rest of the problems.

You haven’t closed the bracket on start so OnTriggerEnter is within it and hence won’t work. Your listener for mouse 0 is wrong and listening for a mouse click within OnTriggerEnter will not work. OnTriggerEnter only runs the code within it on the frame at which the trigger is entered so unless you were to click at the exact frame that it collides it will not work.

My JavaScript is a little rusty but this is what you wanted:

public var light : GameObject;
private var triggerCheck : bool;
private var toggleLight : bool;
 
function Start()
{
   light.SetActive(false);
}
  
function OnTriggerEnter(other : Collider)
{
     triggerCheck = true;
}

function OnTriggerExit(other : Collider)
{
     triggerCheck = false;
}

function Update()
{
       if (Input.GetMouseButtonDown(0))
       {
          if(triggerCheck == true)
          {
               //This can be reset here if you only want to be able to turn it on or off the first time you enter the trigger
               //triggerCheck = false;
               //Otherwise you will want to set this to false only on leaving the trigger

               toggleLight = !toggleLight;
               light.SetActive(toggleLight);
          }
      }
}

That will only allow you to turn the light on with a left click after something has entered the trigger of the object - which is what your code above was attempting. Note that this is not looking specifically for light objects though, if this script is on your player then you would want to check the tag of the other object to see if it is a light.

toggleLight = !toggleLight; is used to toggle the boolean state of SetActive to turn it on or off.

Without knowing more of what you were trying to do though I cannot be of any more help. If you are looking to just click the object to turn the light on instead then you want to use this instead of OnTriggerEnter and the if input:

function OnMouseDown () {
     light.SetActive(true);
}

Hopefully this is enough to learn from though and hopefully you’ll appreciate the effort…