I got to where I connect and be in the same lobby as somebody else, but when I start the game we both load the level but with it’s our own level and it’s just like singleplayer. I don’t know how to fix it. I want us to both have our own camera and be able to control our own player and be able to see each other. I would really appreciate any help or somewhere to get help. Thanks
MenuManager.cs
using UnityEngine;
using System.Collections;
public class MenuManager : MonoBehaviour
{
public MenuManager instance;
public string CurrentMenu;
public string MatchName;
public string MatchPassword = "";
public int MatchMaxPlayers = 4;
private Vector2 ScrollLobby = Vector2.zero;
private string iptemp = "127.0.0.1";
public GUIStyle GameTitleGUIStyle;
public GUIStyle MenuLabelGUIStyle;
public GUIStyle MenuButtonsGUIStyle;
public GUIStyle WordsGUIStyle;
public GUIStyle SmallButtonGUIStyle;
public GUIStyle colorBlue;
public GUIStyle colorBlueL;
public GUIStyle colorGreen;
public GUIStyle colorGreenL;
public GUIStyle colorPink;
public GUIStyle colorRed;
public GUIStyle colorYellow;
public GUIStyle colorPurple;
public GUIStyle colorOrange;
public GUIStyle colorBrown;
public GUIStyle mapName;
public GUIStyle textBox;
void Start()
{
instance = this;
CurrentMenu = "GMain";
MatchName = "Cubes " + Random.Range(0, 5000);
}
void OnGUI()
{
if (CurrentMenu == "GMain")
Game_Main();
if (CurrentMenu == "SPMain")
SP_Main();
if(CurrentMenu == "Main")
Menu_Main();
if (CurrentMenu == "About")
Menu_About();
if (CurrentMenu == "loGame")
Load_Game();
if (CurrentMenu == "Lobby")
Menu_Lobby();
if (CurrentMenu == "Host")
Menu_HostGame();
if (CurrentMenu == "ChoMap")
Menu_ChooseMap();
}
public void NavigateTo(string nextmenu)
{
CurrentMenu = nextmenu;
}
private void Game_Main()
{
GUI.Box(new Rect(Screen.width - 875, Screen.height - 550, 200, 50), "Cubes", GameTitleGUIStyle);
if (GUI.Button(new Rect(Screen.width - 875, Screen.height - 475, 200, 20), "Singleplayer", MenuButtonsGUIStyle))
{
NavigateTo("SPMain");
}
if (GUI.Button(new Rect(Screen.width - 875, Screen.height - 440, 200, 20), "Multiplayer", MenuButtonsGUIStyle))
{
NavigateTo("Main");
}
if (GUI.Button(new Rect(Screen.width - 875, Screen.height - 405, 200, 20), "About", MenuButtonsGUIStyle))
{
NavigateTo("About");
}
if (GUI.Button(new Rect(Screen.width - 875, Screen.height - 370, 200, 20), "Quit Game", MenuButtonsGUIStyle))
{
Application.Quit();
}
}
private void SP_Main()
{
GUI.Box(new Rect(10, 10, 200, 50), "Singleplayer", MenuLabelGUIStyle);
//Buttons Main Menu
if (GUI.Button(new Rect(10, 60, 200, 20), "New Game", MenuButtonsGUIStyle))
{
Application.LoadLevel("testLevel");
}
if (GUI.Button(new Rect(10, 95, 200, 20), "Load Game", MenuButtonsGUIStyle))
{
NavigateTo("loGame");
}
if (GUI.Button(new Rect(10, 130, 200, 20), "Main Menu", MenuButtonsGUIStyle))
{
NavigateTo("GMain");
}
}
private void Load_Game()
{
GUI.Label(new Rect(10, 70, 200, 50), "A work in progress. Nothing here yet.", WordsGUIStyle);
if (GUI.Button(new Rect(10, 130, 200, 50), "Back", MenuButtonsGUIStyle))
{
NavigateTo("SPMain");
}
}
private void Menu_About()
{
if (GUI.Button(new Rect(10, 10, 200, 20), "Back", MenuButtonsGUIStyle))
{
NavigateTo("GMain");
}
GUI.Label(new Rect(10, 70, 200, 50), "A 2.5D platformer. You are a cube and you jump across platforms.", WordsGUIStyle);
GUI.Label(new Rect(10, 90, 200, 50), "© 2013 Tre Babcock", WordsGUIStyle);
}
private void Menu_Main()
{
GUI.Box(new Rect(10, 10, 200, 50), "Multiplayer", MenuLabelGUIStyle);
//Buttons Main Menu
if (GUI.Button(new Rect(10, 60, 200, 20), "Create Game", MenuButtonsGUIStyle))
{
NavigateTo("Host");
}
if (GUI.Button(new Rect(10, 95, 200, 20), "Refresh Games", MenuButtonsGUIStyle))
{
MasterServer.RequestHostList("Co-Op");
}
GUI.Label(new Rect(250, 75, 130, 20), "Player Name", WordsGUIStyle);
MultiplayerManager.instance.PlayerName = GUI.TextField(new Rect(420, 70, 150, 30), MultiplayerManager.instance.PlayerName, textBox);
if (GUI.Button(new Rect(590, 75, 100, 30), "Save Name", SmallButtonGUIStyle))
{
PlayerPrefs.SetString("PlayerName", MultiplayerManager.instance.PlayerName);
}
GUI.Label(new Rect(250, 115, 130, 20), "Direct Connect", WordsGUIStyle);
iptemp = GUI.TextField(new Rect(420, 110, 150, 30), iptemp, textBox);
if (GUI.Button(new Rect(590, 115, 100, 30), "Connect", SmallButtonGUIStyle))
{
Network.Connect(iptemp, 2550);
}
GUILayout.BeginArea(new Rect(Screen.width - 400, 0, 400, Screen.height), "Games List", "Box");
GUILayout.Space(20);
foreach (HostData match in MasterServer.PollHostList())
{
GUILayout.BeginHorizontal("Box");
GUILayout.Label(match.gameName);
if (GUILayout.Button("Connect", MenuButtonsGUIStyle))
{
Network.Connect(match);
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
if (GUI.Button(new Rect(10, 130, 200, 20), "Main Menu", MenuButtonsGUIStyle))
{
NavigateTo("GMain");
}
}
private void Menu_HostGame()
{
GUI.Box(new Rect(10, 10, 200, 50), "Create Game", MenuLabelGUIStyle);
//Buttons Host Game
if (GUI.Button(new Rect(10, 130, 200, 20), "Back", MenuButtonsGUIStyle))
{
NavigateTo("Main");
}
if (GUI.Button(new Rect(10, 60, 200, 20), "Start Server", MenuButtonsGUIStyle))
{
MultiplayerManager.instance.StartServer(MatchName, MatchPassword, MatchMaxPlayers);
}
if (GUI.Button(new Rect(10, 95, 200, 20), "Choose Level", MenuButtonsGUIStyle))
{
NavigateTo("ChoMap");
}
GUI.Label(new Rect(250, 75, 130, 30), "Game Name", WordsGUIStyle);
MatchName = GUI.TextField(new Rect(440, 70, 200, 30), MatchName, textBox);
GUI.Label(new Rect(250, 115, 130, 30), "Game Password", WordsGUIStyle);
MatchPassword = GUI.PasswordField(new Rect(440, 110, 200, 30), MatchPassword, '*', textBox);
GUI.Label(new Rect(250, 150, 130, 30), "Max Players", WordsGUIStyle);
GUI.Label(new Rect(440, 150, 200, 30), (MatchMaxPlayers + 1).ToString());
MatchMaxPlayers = Mathf.Clamp(MatchMaxPlayers, 1, 3);
if (GUI.Button(new Rect(465, 150, 15, 15), "-", SmallButtonGUIStyle))
MatchMaxPlayers -= 1;
if (GUI.Button(new Rect(490, 150, 15, 15), "+", SmallButtonGUIStyle))
MatchMaxPlayers += 1;
GUI.Box(new Rect(440, 10, 100, 30), MultiplayerManager.instance.CurrentMap.MapName, mapName);
}
private void Menu_Lobby()
{
ScrollLobby = GUILayout.BeginScrollView(ScrollLobby, GUILayout.MaxWidth(200));
foreach (MPPlayer pl in MultiplayerManager.instance.PlayerList)
{
if (pl.PlayerNetwork == Network.player)
GUILayout.Box(pl.PlayerName);
}
GUILayout.EndScrollView();
GUI.Box(new Rect(300, 10, 150, 40), MultiplayerManager.instance.CurrentMap.MapName, mapName);
if (Network.isServer)
{
if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 80, 200, 40), "Start Game", MenuButtonsGUIStyle))
{
MultiplayerManager.instance.networkView.RPC("Client_LoadMultiplayerMap", RPCMode.All, MultiplayerManager.instance.CurrentMap.MapLoadName, MultiplayerManager.instance.oldprefix + 1);
MultiplayerManager.instance.oldprefix += 1;
MultiplayerManager.instance.IsMatchStarted = true;
}
}
if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 40, 200, 40), "Disconnect", MenuButtonsGUIStyle))
{
Network.Disconnect();
}
}
private void Menu_ChooseMap()
{
if (GUI.Button(new Rect(10, 10, 200, 50), "Back", MenuButtonsGUIStyle))
{
NavigateTo("Host");
}
GUI.Label(new Rect(220, 10, 130, 30), "Choose Level", WordsGUIStyle);
GUILayout.BeginArea(new Rect(350, 10, 150, Screen.height));
foreach (MapSetting map in MultiplayerManager.instance.MapList)
{
if (GUILayout.Button(map.MapName, SmallButtonGUIStyle))
{
NavigateTo("Host");
MultiplayerManager.instance.CurrentMap = map;
}
}
GUILayout.EndArea();
}
void OnServerInitialized()
{
NavigateTo("Lobby");
}
void OnConnectedToServer()
{
NavigateTo("Lobby");
}
void OnDisconnectedFromServer(NetworkDisconnection info)
{
NavigateTo("Main");
}
}
MultiplayerManager.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiplayerManager : MonoBehaviour
{
public static MultiplayerManager instance;
public string PlayerName;
private string MatchName = "";
private string MatchPassword = "";
private int MatchMaxUsers = 32;
public List<MPPlayer> PlayerList = new List<MPPlayer>();
public List<MapSetting> MapList = new List<MapSetting>();
public MapSetting CurrentMap = null;
public int oldprefix;
public bool IsMatchStarted = false;
void Start()
{
instance = this;
PlayerName = PlayerPrefs.GetString("PlayerName");
CurrentMap = MapList[0];
}
void FixedUpdate()
{
instance = this;
}
void Update()
{
if (IsMatchStarted Input.GetKey(KeyCode.Tab))
{
}
}
public void StartServer(string servername, string serverpassword, int maxusers)
{
MatchName = servername;
MatchPassword = serverpassword;
MatchMaxUsers = maxusers;
Network.InitializeServer(MatchMaxUsers, 2550, false);
MasterServer.RegisterHost("Co-Op", MatchName, "");
}
void OnServerInitialized()
{
Server_PlayerJoinRequest(PlayerName, Network.player);
}
void OnConnectedToServer()
{
networkView.RPC("Server_PlayerJoinRequest", RPCMode.Server, PlayerName, Network.player);
}
void OnPlayerDiconnected(NetworkPlayer id)
{
networkView.RPC("Client_RemovePlayer", RPCMode.All, id);
}
void OnPlayerConnected(NetworkPlayer player)
{
foreach (MPPlayer pl in PlayerList)
{
networkView.RPC("Client_AddPlayerToList", player, pl.PlayerName, pl.PlayerNetwork);
}
networkView.RPC("Client_AddPlayerToList", player, CurrentMap.MapName, "", "");
}
void OnPlayerDisconnectedFromServer()
{
PlayerList.Clear();
}
[RPC]
void Server_PlayerJoinRequest(string playername, NetworkPlayer view)
{
networkView.RPC("Client_AddPlayerToList", RPCMode.All, playername, view);
}
[RPC]
void Client_AddPlayerToList(string playername, NetworkPlayer view)
{
MPPlayer tempplayer = new MPPlayer();
tempplayer.PlayerName = playername;
tempplayer.PlayerNetwork = view;
PlayerList.Add(tempplayer);
}
[RPC]
void Client_RemovePlayer(NetworkPlayer view)
{
MPPlayer temppl = null;
foreach(MPPlayer pl in PlayerList)
{
if (pl.PlayerNetwork == view)
{
temppl = pl;
}
}
if(temppl != null)
{
PlayerList.Remove(temppl);
}
}
[RPC]
void Client_GetMultiplayerMatchSettings(string map, string mode, string others)
{
CurrentMap = GetMap(map);
}
public MapSetting GetMap(string name)
{
MapSetting get = null;
foreach(MapSetting st in MapList)
{
if (st.MapName == name)
{
get = st;
break;
}
}
return get;
}
[RPC]
void Client_LoadMultiplayerMap(string map, int prefix)
{
Network.SetLevelPrefix(prefix);
Application.LoadLevel(map);
}
}
[System.Serializable]
public class MPPlayer
{
public string PlayerName = "";
public NetworkPlayer PlayerNetwork;
}
[System.Serializable]
public class MapSetting
{
public string MapName;
public string MapLoadName;
public Texture MapLoadTexture;
}