- Home /
OnGUI not recognizing variable change
Ive got a GUI setup to let a player start a game server or to refresh the list of games from the unity master server (case 0). When the player hits "Start Server", my int variable gameGuiState changes to 1, the join server and refresh buttons disappear like they should as the case is no longer 0. When the player joins a game, the button increments gameGuiState to 1 but the switch doesn't switch off. I can't figure this out, I am sure its something completely stupid. It isnt throwing any errors or anything
function OnGUI(){
switch (gameGuiState)
{
case 0:
if(GUI.Button(Rect(ButtonX,ButtonY,ButtonW,ButtonH),"Start Server")){
Debug.Log("Starting Server");
StartServer(); //gameGuiState is incremented in this function
}
if(GUI.Button(Rect(ButtonX,ButtonY * 1.2 + ButtonH,ButtonW,ButtonH),"Refresh")){
Debug.Log("Refreshing");
RefreshHostList();
}
if(hostData){
for (var i : int = 0; i < hostData.length; i++){
if(GUI.Button(Rect(ButtonX * 1.5 +ButtonW, ButtonY * 1.2 + (ButtonH * i), ButtonW * 3, ButtonH * 0.5), hostData[i].gameName)){
Network.Connect(hostData[i]);
Debug.Log("JOINING");
gameGuiState = 1;
}
}
}
}
}
Answer by violence · Dec 09, 2013 at 03:12 PM
I fixed the problem but once again I don't quite understand why. In the update function I had this:
if(!Network.isClient && !Network.isServer){gameGuiState=0;}
So it looks like the update function was setting gameGuiState back to zero before it actually connected to the game. I just moved this line from update to start.
Your answer
Follow this Question
Related Questions
Android device not connecting to network 0 Answers
Passing a variable from a client to server 1 Answer
Switch Statements 1 Answer
000webhost master server 0 Answers
Unity networking tutorial? 6 Answers