- Home /
GUI Score Overlapping
I have three lines of text displaying the score, player lives, and player power in my game. In the editor, it plays perfectly fine, but when I build the game, the moment one of the values is updated, it looks like there are five or six numbers being displayed at the same time.
This is what it looks like:
And this is the code controlling the GUI. It's attached to the camera for that level.
void OnGUI() {
float startingPosition = Screen.width - (Screen.width / 4);
GUILayout.BeginArea(new Rect(startingPosition, 0, Screen.width / 4, Screen.height));
GUILayout.BeginHorizontal();
GUILayout.BeginVertical();
GUILayout.Label("Score", customSidebar);
GUILayout.Label("Lives", customSidebar);
GUILayout.Label("Power", customSidebar);
GUILayout.EndVertical();
GUILayout.BeginVertical();
GUILayout.Label(PlayerPrefs.GetInt("score") + "", customSidebar);
GUILayout.Label(PlayerPrefs.GetInt("playerLives") + "", customSidebar);
GUILayout.Label(PlayerPrefs.GetInt("playerPower") + "", customSidebar);
GUILayout.EndVertical();
GUILayout.EndHorizontal();
if (GUILayout.Button("Main Menu", GUILayout.Height(50)))
Application.LoadLevel(0);
GUILayout.EndArea();
It works fine in the editor, is there any reason why it would work there and not work when I build the game?
Could it be you have the script on two objects and while playing in the editor they happen to be ok but on build, one is slighlty getting different position values?
Nope, it's only on the scene camera, and there's only one of those.
And even if OnGUI is being called more than once per frame (which it probably is), it shouldn't be changing the position values.
All my position values are relative to the screen size, and unless the screen size is shifting really REALLY subtly every half a frame or so, it shouldn't be changing the position, so I have no idea why it's doing this...
Huh I figured it out, apparently it was the number of draw calls, I just reduced it to one line
Answer by GlitteringStone · Jul 03, 2013 at 02:52 PM
Hm figured it out, I just reduced the number of GUI.Label functions down to one with some string formatting and it works.
GUI.Label(new Rect(Screen.width * 0.75f, 0, Screen.width * 0.125f, Screen.height * 0.1f), "Score " + PlayerPrefs.GetInt("score") + "\nLives " + PlayerPrefs.GetInt("playerLives") + "\nPower " + PlayerPrefs.GetInt("playerPower"), customSidebar);
Your answer
Follow this Question
Related Questions
Why is my canvas not going away on scene load in build, but is in editor? 2 Answers
GUI not working in Build? 1 Answer
Distribute terrain in zones 3 Answers
GUI Question 0 Answers
OnGUI() works in editor, but not in build with an odd twist! 2 Answers