Well I have a lobby system which works fine, I can host games find and join games and I can chat when I joined a game so the connection is there. But for some reason when the host starts the game the host will load a level but the client(s) will stay in the chat. I have tried two methods (both are at the bottom of the class) but that doesn’t seem to work. Can somebody tell me if those two methods are both wrong and if not I hope you can help me figure out what is wrong.
I also sometimes get the error:
Empty game type given during host registration, aborting
UnityEngine.MasterServer:RegisterHost(String, String)
but since this doesn’t happen all the time i don’t think that’s the full problem
using UnityEngine;
using System;
using System.Collections;
public class GameLobby : MonoBehaviour
{
public int serverPort = 45671;
public string gameName = “”;
bool launchingGame = false;
bool showMenu = false;
public bool useNAT = false;
private ArrayList playerList = new ArrayList();
class PlayerInfo
{
public string username;
public NetworkPlayer player;
}
private int serverMaxPlayers = 4;
private string serverTitle = “Loading…”;
private bool serverPasswordProtected = false;
private string playerName = “”;
private MainMenu mainMenuScript;
public float lastRegTime = -60f;
public string[ ] levels = new string[5];
public string selectedLevel = “”;
void Start()
{
selectedLevel = “”;
showMenu = false;
mainMenuScript = GetComponent();
}
public void EnableLobby()
{
playerName = PlayerPrefs.GetString(“playerName”);
lastRegTime = Time.time - 3600;
launchingGame = false;
showMenu = true;
LobbyChat chat = (LobbyChat)GetComponent(“LobbyChat”);
chat.ShowChatWindow();
}
void OnGUI()
{
if (!showMenu)
return;
if (GUI.Button(new Rect(40, 10, 150, 20), “Back to main menu”))
StartCoroutine(LeaveLobby());
if (launchingGame)
launchingGameGUI();
else if (!Network.isServer !Network.isClient)
hostSettings();
else
showLobby();
}
IEnumerator LeaveLobby()
{
if (Network.isServer || Network.isClient)
{
if (Network.isServer)
MasterServer.UnregisterHost();
Network.Disconnect();
yield return new WaitForSeconds(.3f);
}
LobbyChat chat = (LobbyChat)GetComponent(“LobbyChat”);
chat.CloseChatWindow();
mainMenuScript.OpenMenu(“multiplayer”);
showMenu = false;
}
private string hostSetting_title = “No server title”;
private int hostSetting_players = 2;
private string hostSetting_password = “”;
void hostSettings()
{
GUI.BeginGroup(new Rect(Screen.width / 2 - 175, Screen.height / 2 - 125, 350, 150));
GUI.Box(new Rect(0, 0, 350, 150), “Server options”);
GUI.Label(new Rect(10, 20, 150, 20), “Server title”);
hostSetting_title = GUI.TextField(new Rect(175, 20, 160, 20), hostSetting_title);
GUI.Label(new Rect(10, 40, 150, 20), “Max. players (2-32)”);
hostSetting_players = Convert.ToInt32(GUI.TextField(new Rect(175, 40, 160, 20), hostSetting_players.ToString()));
GUI.Label(new Rect(10, 60, 150, 20), “Password”);
hostSetting_password = GUI.TextField(new Rect(175, 20, 160, 20), hostSetting_password);
GUI.Label(new Rect(10, 80, 150, 20), “Current Level”);
GUI.Label(new Rect(175, 80, 100, 100), selectedLevel);
if (GUI.Button(new Rect(100, 115, 150, 20), “Go to Lobby”))
StartHost(hostSetting_password, Convert.ToInt32(hostSetting_players), hostSetting_title);
GUI.EndGroup();
GUI.Box(new Rect(Screen.width / 4 - 50, Screen.height / 2 - 100, 100, 200), “Maps:”);
int i = 0;
foreach (string level in levels)
{
if (GUI.Button(new Rect(Screen.width / 4 - 40, Screen.height / 2 - 80 + (i * 30), 80, 20), level))
selectedLevel = level;
i++;
}
if (GUI.Button(new Rect(Screen.width / 4 - 40, Screen.height / 2 - 80 + (4 * 30), 80, 20), “Grid”))
{
selectedLevel = “Grid”;
}
}
void StartHost(string password, int players, string serverName)
{
if (players < 1)
players = 1;
if (players >= 32)
players = 32;
if (password != “”)
{
serverPasswordProtected = true;
Network.incomingPassword = password;
}
else
{
serverPasswordProtected = false;
Network.incomingPassword = “”;
}
serverTitle = serverName;
Network.InitializeSecurity();
Network.InitializeServer((players - 1), serverPort, useNAT);
MasterServer.RegisterHost(gameName, hostSetting_title, “No description”);
}
void showLobby()
{
string players = “”;
int currentPlayerCount = 0;
foreach (PlayerInfo playerInstance in playerList)
{
players = playerInstance.username + “\n” + players;
currentPlayerCount++;
}
GUI.BeginGroup(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 200, 400, 180));
GUI.Box(new Rect(0, 0, 400, 180), “Game lobby”);
string pProt = “no”;
if (serverPasswordProtected) pProt = “yes”;
GUI.Label(new Rect(10, 20, 150, 20), “Password Protected”);
GUI.Label(new Rect(150, 20, 100, 100), pProt);
GUI.Label(new Rect(10, 40, 150, 20), “Server title”);
GUI.Label(new Rect(150, 40, 100, 100), serverTitle);
GUI.Label(new Rect(10, 60, 150, 20), “Players”);
GUI.Label(new Rect(150, 60, 100, 100), currentPlayerCount.ToString() + “/” + serverMaxPlayers.ToString());
GUI.Label(new Rect(10, 80, 150, 20), “Level”);
GUI.Label(new Rect(150, 80, 100, 100), selectedLevel);
GUI.Label(new Rect(10, 100, 150, 20), “Current Players”);
GUI.Label(new Rect(150, 100, 100, 100), players);
if (Network.isServer)
{
if (GUI.Button(new Rect(25, 140, 150, 20), “Start Game”))
HostLaunchGame();
}
else
{
GUI.Label(new Rect(25, 140, 200, 40), “Waiting for the server to start the game…”);
}
GUI.EndGroup();
}
void OnConnectedToServer()
{
//called on client
//sends everyone this clients data
playerList = new ArrayList();
playerName = PlayerPrefs.GetString(“playerName”);
networkView.RPC(“addPlayer”, RPCMode.AllBuffered, Network.player, playerName);
}
void OnServerInitialized()
{
//called on host
//add hosts own data to playerlist
playerList = new ArrayList();
networkView.RPC(“addPlayer”, RPCMode.AllBuffered, Network.player, playerName);
bool pProt = false;
if (Network.incomingPassword != “”)
pProt = true;
int maxPlayers = Network.maxConnections + 1;
networkView.RPC(“setServerSettings”, RPCMode.AllBuffered, pProt, maxPlayers, hostSetting_title);
}
void Update()
{
if (Network.isServer lastRegTime < Time.time - 60)
{
lastRegTime = Time.time;
MasterServer.RegisterHost(gameName /“MyGame”/, hostSetting_title);
}
}
[RPC]
void setServerSettings(bool password, int maxPlayers, string newServerTitle)
{
serverMaxPlayers = maxPlayers;
serverTitle = newServerTitle;
serverPasswordProtected = password;
}
void OnPlayerDisconnected(NetworkPlayer player)
{
//called on host
//remove player infromation from playerlist
networkView.RPC(“playerLeft”, RPCMode.All, player);
LobbyChat chat = (LobbyChat)GetComponent(“LobbyChat”);
chat.addGameChatMessage(“A player left the lobby”);
}
[RPC]
void addPlayer(NetworkPlayer player, string username)
{
Debug.Log("got addplayer " + username);
PlayerInfo playerInstance = new PlayerInfo();
playerInstance.player = player;
playerInstance.username = username;
playerList.Add(playerInstance);
}
[RPC]
void playerLeft(NetworkPlayer player)
{
PlayerInfo deletePlayer = new PlayerInfo();
foreach (PlayerInfo playerInstance in playerList)
{
if (player == playerInstance.player)
deletePlayer = playerInstance;
}
playerList.Remove(deletePlayer);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
void HostLaunchGame()
{
if (!Network.isServer)
return;
Network.maxConnections = -1;
MasterServer.UnregisterHost();
networkView.RPC(“launchGame”, RPCMode.All);
}
[RPC]
void launchGame()
{
Network.isMessageQueueRunning = false;
launchingGame = true;
}
void launchingGameGUI()
{
GUI.Box(new Rect(Screen.width / 4 + 180, Screen.height / 2 - 30, 280, 50), “”);
if (Application.CanStreamedLevelBeLoaded(selectedLevel))
{
GUI.Label(new Rect(Screen.width / 4 + 200, Screen.height / 2 - 25, 285, 150), "Loaded, starting the game! " + selectedLevel);
Application.LoadLevel(selectedLevel);
}
else
{
GUI.Label(new Rect(Screen.width / 4 + 200, Screen.height / 2 - 25, 285, 150), "Starting… Loading the game: " + Mathf.Floor(Application.GetStreamProgressForLevel(selectedLevel) * 100) + “%”);
}
}
[RPC]
public void LoadLevel(string level, int levelPrefix)
{
// omitted code
Network.SetSendingEnabled(0, false);
Network.isMessageQueueRunning = false;
Network.SetLevelPrefix(levelPrefix);
Application.LoadLevel(level);
//wait till level is loaded
new WaitForSeconds(2);
Debug.Log(level);
// Allow receiving data again
Network.isMessageQueueRunning = true;
// Now the level has been loaded and we can start sending out data
Network.SetSendingEnabled(0, true);
// Notify our objects that the level and the network is ready
foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
{
go.SendMessage(“OnNetworkLoadedLevel”, SendMessageOptions.DontRequireReceiver);
Debug.Log(“I’m in”);
}
}
}