Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eyecontrol · Jul 03, 2012 at 01:48 PM · vector3distanceonmousedownactivateclickable

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Piflik · Jul 03, 2012 at 01:57 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eyecontrol · Jul 03, 2012 at 02:06 PM 0
Share

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

avatar image
0

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"

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eyecontrol · Jul 03, 2012 at 03:45 PM 0
Share

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?

avatar image Bunny83 · Jul 04, 2012 at 01:07 AM 0
Share

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.

avatar image eyecontrol · Jul 04, 2012 at 07:52 AM 0
Share

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?

avatar image Bunny83 · Jul 04, 2012 at 09:44 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges