OnMouseDown

I have a problem regarding the OnMouseDown function.
When I click the object nothing happends.

This is my Javascript.

#pragma strict
var player : Transform;
var alight : GameObject;
var isnear : boolean;



function OnMouseDown() {
		if (isnear == true){
		alight.light.enabled =!alight.light.enabled;
		}
		else {
		}
	}

	function OnTriggerEnter () {
		isnear = true;
	}
	
		function OnTriggerExit () {
		isnear = false;
	}

Does your object has a Collider? And it has a checked IsTrigger property?

Yes, I get output when I click the button but no result with the lights.

Could be anything.

isnear could be false
alight could be null
alight.light could be null
the object this script is attached to could not have a collider
the object this script is attached to could not have a collider marked as a trigger
the thing entering the collider attached to this object could not have a rigidbody
the thing entering the collider attached to this object could not have a collider

isnear could be false - works
alight could be null - works
alight.light could be null - works
the object this script is attached to could not have a collider - works
the object this script is attached to could not have a collider marked as a trigger - works
the thing entering the collider attached to this object could not have a rigidbody - not sure if the players needs that
the thing entering the collider attached to this object could not have a collider - works

If he doesn’t have a rigidbody then the triggers probably aren’t firing and isnear is never set to true. Putting some Debug statements at various points in your code would sort it out rather quickly.

The code doesn’t seem to register clicks, it does register when the player enters or exits the trigger.

#pragma strict
var player : Transform;
var alight : GameObject;
var isnear : boolean;




function OnMouseDown() {
		if (isnear == true){
		alight.light.enabled =!alight.light.enabled;
		Debug.Log("Everything should work");
		}
	Debug.Log("Clicked");
	}

	function OnTriggerEnter () {
		isnear = true;
		Debug.Log("Player is near");
	}
	
		function OnTriggerExit () {
		isnear = false;
		Debug.Log("Player isn't near");
	}

If the camera is inside the bounds of the collider then it won’t register clicks because the raycast won’t hit it.

So how can I fix it?

Poll Input.GetMouseButtonDown inside an Update method when isnear is true.