My GUI is looks shit when I Build and Run my game but looks fine in the editor

Heres a video of my problem:

As you can see everything is fine in the editor but in the built version theres a ton of stuff wrong!

Stuff wrong in the built version:

  • Every GUILayout.Label looks weird in the built version.
  • GUI.Windows are still visable behind the Main Menu’s buttons.
  • Everything looks weird.

Please help guys!!! Thanks for reading!

Edit: Changed link from vid.me to youtube.com because vid.me made the video have terrible quality.

Has nobody ever had this before?

You have to show the code if you want help. The old GUI can be very tricky and sensitive when it comes to code execution paths.

1 Like

Sorry heres the code:

using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;

public class NetworkManager : MonoBehaviour {

    public GUISkin MainMenuGUISkin;

    private bool showStartServer;
    private bool showJoinServer;

    private string startServerPort;
    private string startServerMaxPlayers;

    private string joinServerIP;
    private string joinServerPort;

    void OnGUI() {

        GUILayout.Label("FPS Prototype - Adam Stuck");

        //Only show the main menu if you are not connected to a server or if you are not hosting a server
        if(!Network.isClient && !Network.isServer) {


            if(!showStartServer && !showJoinServer) {

                GUI.skin = MainMenuGUISkin;

                GUI.Window(0, new Rect(Screen.width/2-(Screen.width/4)/2, 0, Screen.width/4, Screen.height), MainMenuWindow, "");

                GUI.skin = null;

            }

            if(showStartServer) {
               
                GUI.Window(1, new Rect(Screen.width/2-200/2, Screen.height/2-275/2, 200, 275), StartServerWindow, "");
               
            }

            if(showJoinServer) {
               
                GUI.Window(2, new Rect(Screen.width/2-200/2, Screen.height/2-275/2, 200, 275), JoinServerWindow, "");
               
            }

        }

    }

    //Main Menu Window
    void MainMenuWindow (int windowID) {

        //Ensures that the buttons are in the middle of the screen
        int middleOfScreen = (Screen.height/9) + (Screen.height/30) + (Screen.height/9) + (Screen.height/30) + (Screen.height/9);

        GUILayout.Space(Screen.height/2 - middleOfScreen/2);

        //Hide main menu and show Start Server window.
        if(GUILayout.Button("Start Server", GUILayout.Height(Screen.height/9))) {

            startServerPort = "26500";
            startServerMaxPlayers = "10";

            showStartServer = true;

        }

        GUILayout.Space(Screen.height/30);

        //Hide main menu and show Join Server window.
        if(GUILayout.Button("Join Server", GUILayout.Height(Screen.height/9))) {

            joinServerIP = "127.0.0.1";
            joinServerPort = "26500";

            showJoinServer = true;

        }

        GUILayout.Space(Screen.height/30);

        //Exit game.
        if(GUILayout.Button("Exit Game", GUILayout.Height(Screen.height/9))) {

            Application.Quit();

        }
       
    }

    //Start Server Window
    void StartServerWindow (int windowID) {

        GUILayout.Label("Port");

        startServerPort = GUILayout.TextField(startServerPort, 5);

        startServerPort = Regex.Replace(startServerPort, "[^0-9]", "");

        GUILayout.Space(20);

        GUILayout.Label("Max Players");
       
        startServerMaxPlayers = GUILayout.TextField(startServerMaxPlayers, 2);

        startServerMaxPlayers = Regex.Replace(startServerMaxPlayers, "[^0-9]", "");

        GUILayout.Space(20);

        //Start the server
        if(GUILayout.Button("Start Server", GUILayout.Height(Screen.height/20))) {
           
            Network.InitializeServer(int.Parse(startServerMaxPlayers), int.Parse(startServerPort), false);
           
        }
       
        GUILayout.Space(10);

        //Go back to main menu.
        if(GUILayout.Button("Go Back", GUILayout.Height(Screen.height/20))) {
           
            showStartServer = false;
           
        }

    }

    //Join Server Window
    void JoinServerWindow (int windowID) {

        GUILayout.Label("IP");

        joinServerIP = GUILayout.TextField(joinServerIP, 15);

        joinServerIP = Regex.Replace(joinServerIP, "[^0-9.]", "");

        GUILayout.Space(20);
       
        GUILayout.Label("Port");
       
        joinServerPort = GUILayout.TextField(joinServerPort, 5);
       
        joinServerPort = Regex.Replace(joinServerPort, "[^0-9]", "");

        GUILayout.Space(20);

        //Connect to server
        if(GUILayout.Button("Connect to Server", GUILayout.Height(Screen.height/20))) {
           
            Network.Connect(joinServerIP, int.Parse(joinServerPort));

        }

        GUILayout.Space(10);

        //Go back to main menu.
        if(GUILayout.Button("Go Back", GUILayout.Height(Screen.height/20))) {
           
            showJoinServer = false;
           
        }
       
    }

}

Old onGUI is fun, I must have 15000 lines of code and 100 Booleans dedicated to exposing and hiding various onGUI windows as appropriate. Your Boolean conditions on when to hide & show onGUI elements isn’t complete.

1 Like

Can you be a bit more specific as to what line I am missing what on please?

Guys seriously?! Nobody knows a solution??? Come on. This is really frustrating and “Your Boolean conditions on when to hide & show onGUI elements isn’t complete” didn’t help me at all. Somebody just tell me what the problem is properly so I can get it fixed. Waiting 2 days just to get some stupid Unity bug fixed with your vague answers is not at all helpful. I didnt post on the forum to find out “Your Boolean conditions on when to hide & show onGUI elements isn’t complete” I posted here because I need an answer!!!

Goat is completely right. There is most likely something wrong with your booleans. A workaround for that issue might be to use the following code:

if(!showStartServer && !showJoinServer) {
  GUI.skin = MainMenuGUISkin;
  GUI.Window(0, new Rect(Screen.width/2-(Screen.width/4)/2, 0, Screen.width/4, Screen.height), MainMenuWindow, "");
  GUI.skin = null;
} else if(showStartServer) {
  GUI.Window(1, new Rect(Screen.width/2-200/2, Screen.height/2-275/2, 200, 275), StartServerWindow, "");
} else if(showJoinServer) {
  GUI.Window(2, new Rect(Screen.width/2-200/2, Screen.height/2-275/2, 200, 275), JoinServerWindow, "");
}

It uses else if to make sure only one of the windows is being drawn at once.

Thanks for the reply but unfortunately this didn’t fix anything :frowning:

I had a look at the new GUI system but it seems like I would need to be able to draw to make a good-looking gui with that.

Did you make sure that the script is only used once in the project?

If you mean there is only one gameobject that has this script then yes.

Is there an active camera rendering something behind your GUI?
It looks like the surface behind your GUI is never cleared. That could explain why the windows are still visible.

I suggest to add a camera rendering a solid color or to use GUI.DrawTexture() to draw some background before the rest of the GUI.