Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MattieMonsterDev · Nov 05, 2016 at 09:22 PM · c#buildprogrammingdistancedistance check

Distance checking not working on build

I have a problem. The distance check works fine in the editor, however, I have to run it, stop it and then run it again for its work. As well as that, it is completely broken on a build (buttons can be pressed from any distance).

The code:

public class UpToLevel : MonoBehaviour { private GameObject player;

 void Update()
 {
     player = GameObject.Find("FPSController");
     var distance = Vector3.Distance(player.transform.position, transform.position);
     //print (distance);
 }

 void OnMouseDown()
 {
     var distance = Vector3.Distance(player.transform.position, transform.position);
     if (distance > 3.0)
     {
         SceneManager.LoadScene("DemoLevel");
     }
 }


Any help? Built to Windows x86_64

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kapel · Nov 06, 2016 at 09:29 AM

Are you sure you have only one FPSController object on scene? Secondly i recommend you to don't use GameObject.Find in Update function for performance reasons. There is no need to do this every frame. Use Start() for this.

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 MattieMonsterDev · Nov 06, 2016 at 12:18 PM 0
Share

Yes, I have only 1 FPSController.

Thank you for the advice, here is the new code:

 void Start()
 {
     player = GameObject.Find("FPSController");
 }

 void Update()
 {
     var distance = Vector3.Distance(player.transform.position, transform.position);
     //print (distance);
 }

 void On$$anonymous$$ouseDown() {
     var distance = Vector3.Distance(player.transform.position, transform.position);
     if (distance > 3.0)
     {
         Scene$$anonymous$$anager.LoadScene("pp_lvl2");
     }
 }

}

avatar image MattieMonsterDev · Nov 06, 2016 at 12:20 PM 0
Share

Also, please remember: This only happens in a windows build! (64 or 32 bit)!

It works fine in the editor's player.

avatar image tanoshimi · Nov 06, 2016 at 12:22 PM 0
Share

You should really correctly type the distance variable - you're calculating a float but then comparing it to a double. And why don't you Debug.Log the distance value in On$$anonymous$$ouseDown rather than in Update?

avatar image MattieMonsterDev tanoshimi · Nov 06, 2016 at 12:52 PM 0
Share

Oh, Sorry! I never even saw that!

I'll change that. I should probably tell you, I'm mostly a beginner.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Calculate the distance between an object and my player 1 Answer

How to drag and drop in 2D game 0 Answers

Jagged Array 2 Answers


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