- Home /
GUI not working in Build?
I have been struggling to work out the problem here.
I am toggling certain GUI elements depending on which buttons you press using booleans. Here is my script:
@script ExecuteInEditMode()
var gameName : String = "thetestgame123";
var refreshing = false;
var hostData : HostData[];
var playerPrefab : GameObject;
var create = false;
var joining = false;
var serverName = "";
var serverPass = "";
var clientPass = "";
function OnGUI () {
if(!Network.isClient && !Network.isServer) {
if(!create && !joining){
if (GUI.Button(Rect(Screen.width/2,Screen.height/2,100,20),"Create Game")) {
create = true;
}
if (GUI.Button(Rect(Screen.width/2,Screen.height/2 + 30,100,20),"Find Game")) {
joining = true;
refreshHostList();
}
}
if (create){
if (GUI.Button(Rect(Screen.width/2,Screen.height/2 + 60,100,20),"Create")) {
startServer();
}
GUI.Label(Rect (Screen.width/2 - 50,Screen.height/2,100,20),"Server Name:");
GUI.Label(Rect (Screen.width/2 + 75,Screen.height/2,100,20),"Password:");
serverName = GUI.TextField (Rect (Screen.width/2 - 60,Screen.height/2 + 30,100,20), serverName, 25);
serverPass = GUI.PasswordField (Rect (Screen.width/2 + 60,Screen.height/2 + 30,100,20), serverPass, "*"[0], 25);
if (GUI.Button(Rect(Screen.width/1.2,Screen.height/20,100,20),"Back")) {
create = false;
}
}
if (joining){
if(hostData) {
for(var i:int = 0; i < hostData.length; i++) {
GUI.Label(Rect(Screen.width/2 - 50,Screen.height/2 + 60,100,20),hostData[i].gameName);
clientPass = GUI.PasswordField (Rect (Screen.width/2 + 60,Screen.height/2 + 30,100,20), clientPass, "*"[0], 25);
if (GUI.Button(Rect(Screen.width/2,Screen.height/2 + 60,100,20),"Join")) {
Network.Connect(hostData[i]);
}
}
}
if (GUI.Button(Rect(Screen.width/1.2,Screen.height/20,100,20),"Back")) {
joining = false;
}
if(!hostData) {
joining = false;
}
}
if (GUI.Button(Rect(Screen.width/20,Screen.height/20,100,20),"Quit")) {
Application.Quit();
}
}
}
function Update () {
if(refreshing) {
if(MasterServer.PollHostList().Length > 0) {
refreshing = false;
hostData = MasterServer.PollHostList();
}
}
}
function startServer () {
Network.incomingPassword = serverPass;
Network.InitializeServer(32,25001, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, serverName, "this is a tutorial");
}
function OnServerInitialized () {
DontDestroyOnLoad (transform.gameObject);
Application.LoadLevel ("Lobby");
lobbySpawn();
}
function OnConnectedToServer () {
lobbySpawn();
}
function lobbySpawn(){
yield WaitForSeconds(0.1);
Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
}
function refreshHostList () {
MasterServer.RequestHostList(gameName);
refreshing = true;
}
The problem lies within the first section of the GUI function, where you press the create game button, the variable is changed to true, which means the button should stop being shown. The second set of 'creating' elements should now be visible.
This only works in the Unity Editor seen here:
However, is doesn't work in the Build seen here:
I have changed the variables around, added a function to delay the change in boolean a little, I have even tried moving the create and join buttons to a different script which destroys itself and created the shown script. All tests came back with exactly the same results. There are no errors when in the Editor. It has no obvious reason as to why it shouldn't work.
Can't recreate this - the menu looks correct in editor or in build when I click on "Create Game" (under Win8 64 bit). What platform are you building to?
Answer by zertach · Nov 07, 2013 at 10:41 PM
Normally for 2D GUI you dont need to camera. but it is a bug or soemthing else i dont know.
but i had same problem. if you deleted Camera from your scene. again create one main camera into your scene and everything will work fine.
The big problem is i;m using the cameras because you are joining a server and your player needs its own camera, I will try without cameras first though
Ahh, the problem was that I didn't have a camera until you joined the server, so the GUI had no camera to work on. Thanks!
Your answer
Follow this Question
Related Questions
Script work in editor but not in build 0 Answers
uGUI objects not Instantiate()ing in executable 0 Answers
GUI works flawlessly in editor, but will not show in standalone build. 1 Answer
GUI Score Overlapping 1 Answer
Why is my canvas not going away on scene load in build, but is in editor? 2 Answers