How to click from specyfic distance?

Hi!
In my FPS project I’m trying to make door open by clicking on them and with a sound and I almoust done it, but I have last one problem. I would like to make, that function OnMouseDown () will be activ only when “Player” gets close to my door. I taged my FPS Controler as a “Player” and I have done animation of a open/close door in Unity3d. And when I’m trying to run preview there is this error in 8th line (if): NullReferenceException: Object reference not set to an instance of an object

this is my Java script:

public var dist:int = 2;
var target : GameObject;
var animation1 : AnimationClip;
var sound1 : AudioClip;


function OnMouseDown () {
 if(Vector3.Distance(GameObject.Find("Player").position, transform.position) <= dist)

 audio.clip = sound1;
 audio.Play ();
 target.animation.clip = animation1;
 target.animation.Play ();
 }

function Update () {

}

please help!
what am I doing wrong?
thx

Try ‘GameObject.FindWithTag(“Player”)’ insted of ‘GameObject.Find(“Player”)’

GameObject.Find (and even FindWithtag) returns a GameObject. A GameObject doesn’t have a position. It has a Transform component which has a position :wink:

GameObject.Find("Player").transform.position

btw, you should avoid any Find methods if possible. It’s better to have a public variable where you can drag your player onto. Like you already did with “target”