x


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

more ▼

asked Jul 03 '12 at 01:48 PM

eyecontrol gravatar image

eyecontrol
0 1 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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"

more ▼

answered Jul 03 '12 at 02:57 PM

Bunny83 gravatar image

Bunny83
45.1k 11 49 206

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 OnMouseDown () {
 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?

Jul 03 '12 at 03:45 PM eyecontrol

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.

Jul 04 '12 at 01:07 AM Bunny83

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 OnMouseDown 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?

Jul 04 '12 at 07:52 AM eyecontrol

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 OnMouseDown ()
{
    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
}
Jul 04 '12 at 09:44 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

Try 'GameObject.FindWithTag("Player")' insted of 'GameObject.Find("Player")'

more ▼

answered Jul 03 '12 at 01:57 PM

Piflik gravatar image

Piflik
5.4k 15 26 44

the same Error apears. It might be a problem that I put 'GameObject' twice in one script, and these should be a diferent objects?

Jul 03 '12 at 02:06 PM eyecontrol
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x573
x354
x120
x79
x25

asked: Jul 03 '12 at 01:48 PM

Seen: 348 times

Last Updated: Jul 04 '12 at 09:57 AM