|
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: please help! what am I doing wrong? thx
(comments are locked)
|
|
GameObject.Find (and even FindWithtag) returns a GameObject. A GameObject doesn't have a position. It has a Transform component which has a 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: 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. 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 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.
Jul 04 '12 at 09:44 AM
Bunny83
(comments are locked)
|
|
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?
Jul 03 '12 at 02:06 PM
eyecontrol
(comments are locked)
|
