- Home /
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
Answer by Piflik · Jul 03, 2012 at 01:57 PM
Try 'GameObject.FindWithTag("Player")' insted of 'GameObject.Find("Player")'
the same Error apears. It might be a problem that I put 'GameObject' twice in one script, and these should be a diferent objects?
Answer by Bunny83 · Jul 03, 2012 at 02:57 PM
GameObject.Find (and even FindWithtag) returns a GameObject. A GameObject doesn't have a position. It has a Transform component which has a position ;)
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"
thx Bunny83, but if you could heplp me more it would be great. I made this one:
public var dist:int = 2;
var player : GameObject;
var target : GameObject;
var animation1 : AnimationClip;
var sound1 : AudioClip;
function On$$anonymous$$ouseDown () {
if(Vector3.Distance(player.transform.position, transform.position) <= dist)
audio.clip = sound1;
audio.Play ();
target.animation.clip = animation1;
target.animation.Play ();
}
function Update () {
}
and I puted FPS Controler onto it. and I achived that there is no Error apears, but still script doesn't work in a distance I put. what is wrong?
What doesn't work. Be more specific.
To sum up: Your script does play the given sound an play the given animation when you click on that object when it's within the range of 2 units. Is that what you want? if not, tell use what you want.
Also tell us what happens when you click the object. Does the sound play? nothing happens? the animation plays?
Note: the "target" object (the one with the animation) need this animation to be in the animations list, otherwise it can't be played.
Yes, everything works. Animation works, sound is playing, but not in the range of 2 units. From one side from the object I have to be like 1,5 unit from it, and from the other side I have to be very close, like I have to touch the object to make function On$$anonymous$$ouseDown works. So it seems like whole script works exept the 'distance'part.
Or maybe the problem is not in the Vector3 but in the position of the FPS Controller, like here: http://answers.unity3d.com/questions/35083/transform-position-not-changing.html I'm useing FPSWalker.js from FPS Controller to move my Player. How can I check if coordinates of my Player are actualy changeing while I'm walkig around the map? and if they not, how can I make them changeing?
I just realised that you don't have any brackets after your if-statement. That way only the next line will be affected which is
audio.clip = sound1;
Everything else is always executed when you click. That's why you should indent your code properly. Also, if you don't use it, remove the Update function.
function On$$anonymous$$ouseDown ()
{
if(Vector3.Distance(GameObject.Find("Player").position, transform.position) <= dist)
{ // You miss this one
audio.clip = sound1;
audio.Play ();
target.animation.clip = animation1;
target.animation.Play ();
} // and this one
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613073629im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I set a maximum distance on an activate-able object? 1 Answer
Move an object to a specific distance 0 Answers
Vector3.Distance not working properly? 3 Answers
Activate Object in a Certain Distance 2 Answers
Find a position by knowing the directionVector and the distance to this point 1 Answer