Distance won't work

Help!!!

This script was supposed to assign a specific distance when clicking an npc to start a connversation.
If “dist” is less than “fixdist”, the character can click to the npc.

This code works fine in my other event, I copy pasted it from there.

Here is the code:

private var trgtObjct : Transform;

function Start(){

	trgtObjct = GameObject.FindGameObjectWithTag("Player").transform;
}

function npcTalk(){

	var fixdist = 100.3872;

	if (trgtObjct!= null)
	{	
		var dist : float = Vector3.Distance(trgtObjct.position, this.transform.position);
	
		if (dist <= fixdist)
		{
			OnMouseDown();
		}
		else
			print("Lumapit ka");

}

sorry for the bad english.

If the distance is smaller, you are explicitly calling OnMouseDown(). However OnMouseDown() is being called unconditionally (well, the condition is that the mouse is over the object’s Collider when clicking) by the game engine on mouse click anyway, so your approach does not work this way.

Use your own function to place your code in that should be executed when you have a “valid” click, and don’t call it “OnMouseDown”.