- Home /
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
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.
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");
}
}
}
Also, please remember: This only happens in a windows build! (64 or 32 bit)!
It works fine in the editor's player.
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?
Oh, Sorry! I never even saw that!
I'll change that. I should probably tell you, I'm mostly a beginner.
Your answer
Follow this Question
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