What am i doing wrong? I want to make a raycast acces panel with lights

I want to make a raycast acces panel with lights

Assigned this to the main camera:

var CODELIGHT : Light;

function Update()
{

flashlight = GetComponent("Light");

}
{
    if (Input.GetKeyDown(KeyCode.E))
    {
        var hit : RaycastHit;
        if (Physics.Raycast(transform.position, transform.forward, hit, 2.0F))
        {
            if(hit.transform.tag == "Obj1")
            {
               {
				
			if (flashlight.enabled) {
			CODELIGHT.enabled = false;
			}
			else {
			CODELIGHT.enabled = true;
			
            }
        }
    }
}

}
} 

hooked the script up to the main camera en made a cube with a boxcollider as trigger, tagged the cube Obj1. And assigned a pointlight to the script, but sadly… No succes:(

First step, find out what (if anything) that ray cast is hitting.

In addition its worth noting that ‘hit.transform’ acceses the rigid body associated with the collider that was hit, which may not be the correct object. Use hit.collider. to access the actual game object with the collider on.

So add Debug.Log(hit.collider.name) inside the ray cast if statement, and see what gets printed.

If it’s hitting the wrong thing (for example the player) you can use ray cast filters (easily found with a google search) to only look at specific layers.

(extra note) - just to be sure, I’m guessing the object with this script attached does have the light component also attached? Easily checked with Debug.Log(flashLight) just to make sure it is actually finding something to turn on!

-Chris

I’m so late with this, but thanks,as I remember correctly, this solved it!