Hey guys Sniper here,
Im making a Multiplayer fps and i have written out the codes (All codes included at bottom) and when i click start match then spawn the server can spawn but the client’s spawn button wont come up
Multiplayer Manager Script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiplayerManager : MonoBehaviour
{
public static MultiplayerManager instance;
public GameObject PlayerManagerPrefab;
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;
//General Multiplayer Modes
public bool MatchLoaded;
public MPPlayer MyPlayer;
public GameObject[] SpawnPoints;
void Start()
{
instance = this;
PlayerName = PlayerPrefs.GetString ("PlayerName");
CurrentMap = MapList[0];
DontDestroyOnLoad(gameObject);
}
void FixedUpdate()
{
instance = this;
}
public void StartServer(string servername, string serverpassword, int maxusers)
{
MatchName = servername;
MatchPassword = serverpassword;
MatchMaxUsers = maxusers;
Network.InitializeServer(MatchMaxUsers, 2550, false);
MasterServer.RegisterHost("DeathMatch", MatchName, "");
//Network.InitializeSecurity();
}
void OnServerInitialized()
{
Server_PlayerJoinRequest(PlayerName, Network.player);
}
void OnConnectedToServer()
{
networkView.RPC ("Server_PlayerJoinRequest", RPCMode.Server, PlayerName, Network.player);
}
void OnPlayerDisconnected(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, "", "");
}
[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);
if(Network.player == view)
{
MyPlayer = tempplayer;
GameObject play = Network.Instantiate(PlayerManagerPrefab, Vector3.zero, Quaternion.identity, 5) as GameObject;
play.GetComponent<PlayerManager>().thisplayer = MyPlayer;
}
}
[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)
{
break;
}
}
return get;
}
[RPC]
void Client_LoadMultiplayerMap(string map, int prefix)
{
//Network.SetLevelPrefix(prefix);
Application.LoadLevel (map);
}
void OnGUI()
{
if(!MyPlayer.PlayerIsAlive IsMatchStarted)
SpawnMenu();
}
[RPC]
void Server_SpawnPlayer(NetworkPlayer player)
{
int numberSpawn = Random.Range(0, SpawnPoints.Length - 1);
networkView.RPC("Client_SpawnPlayer", RPCMode.All, player, SpawnPoints[numberSpawn].transform.position + new Vector3(0,2,0), SpawnPoints[numberSpawn].transform.rotation);
}
[RPC]
void Client_SpawnPlayer(NetworkPlayer player, Vector3 position, Quaternion rotation)
{
MultiplayerManager.GetMPPlayer(player).PlayerIsAlive = true;
MultiplayerManager.GetMPPlayer(player).PlayerHealth = 100;
if(player == MyPlayer.PlayerNetwork)
{
MyPlayer.PlayerManager.ControllerTransform.position = position;
MyPlayer.PlayerManager.ControllerTransform.rotation = rotation;
MyPlayer.PlayerManager.networkView.RPC("Client_PlayerAlive", RPCMode.All);
}
else
{
}
}
void OnLevelWasLoaded()
{
if(Application.loadedLevelName == CurrentMap.MapLoadName Network.isServer)
{
MatchLoaded = true;
SpawnPoints = GameObject.FindGameObjectsWithTag("Spawn Point");
networkView.RPC("Client_ServerLoaded", RPCMode.AllBuffered);
}
}
[RPC]
void Client_ServerLoaded()
{
MatchLoaded = true;
}
public static MPPlayer GetMPPlayer(NetworkPlayer player)
{
foreach(MPPlayer play in MultiplayerManager.instance.PlayerList)
{
if(play.PlayerNetwork == player)
{
return play;
}
}
return null;
}
//DeathMatch
void SpawnMenu()
{
if(GUI.Button(new Rect(5, Screen.height - 40, 250, 35), "Spawn"))
{
if(Network.isServer)
Server_SpawnPlayer(Network.player);
else
networkView.RPC("Client_SpawnPlayer", RPCMode.Server, Network.player);
}
}
}
[System.Serializable]
public class MPPlayer
{
public string PlayerName = "";
public NetworkPlayer PlayerNetwork;
public PlayerManager PlayerManager;
public int PlayerHealth = 100;
public bool PlayerIsAlive;
public int PlayerKills;
public int PlayerDeaths;
}
[System.Serializable]
public class MapSetting
{
public string MapName;
public string MapLoadName;
public Texture MapLoadTexture;
}
Player Manager Script
using UnityEngine;
using System.Collections;
public class PlayerManager : MonoBehaviour
{
public MPPlayer thisplayer;
public PlayerController Controller;
public Transform ControllerTransform;
public GameObject OutsideView;
public Vector3 CurrentPosition;
public Quaternion CurrentRotation;
void Start()
{
DontDestroyOnLoad(gameObject);
OutsideView.SetActiveRecursively(false);
ControllerTransform.gameObject.SetActiveRecursively(false);
thisplayer = MultiplayerManager.GetMPPlayer(networkView.owner);
thisplayer.PlayerManager = this;
}
void FixedUpdate()
{
if(networkView.isMine)
{
CurrentPosition = ControllerTransform.position;
CurrentRotation = ControllerTransform.rotation;
}
else
{
ControllerTransform.position = CurrentPosition;
ControllerTransform.rotation = CurrentRotation;
}
}
void OnSerializeNeworkView(BitStream stream, NetworkMessageInfo info)
{
if(stream.isWriting)
{
stream.Serialize(ref CurrentPosition);
stream.Serialize(ref CurrentRotation);
}
else
{
stream.Serialize(ref CurrentPosition);
stream.Serialize(ref CurrentRotation);
}
}
void HandleBulletDamage(int damage)
{
if(Network.isServer)
Server_HandleBulletDamage(damage);
else
networkView.RPC("Server_HandleBulletDamage", RPCMode.Server, damage);
}
[RPC]
public void Server_HandleBulletDamage(int damage)
{
thisplayer.PlayerHealth -= damage;
if(thisplayer.PlayerHealth <= 0)
{
thisplayer.PlayerIsAlive = false;
thisplayer.PlayerHealth = 0;
networkView.RPC("Client_PlayerDead", RPCMode.All);
}
else
{
}
}
[RPC]
public void Client_PlayerDead()
{
OutsideView.SetActiveRecursively(false);
if(networkView.isMine)
{
ControllerTransform.gameObject.SetActiveRecursively(false);
}
}
[RPC]
public void Client_PlayerAlive()
{
OutsideView.SetActiveRecursively(true);
if(networkView.isMine)
{
ControllerTransform.gameObject.SetActiveRecursively(true);
}
else
{
OutsideView.SetActiveRecursively(true);
}
}
}
Menu Manager Script
using UnityEngine;
using System.Collections;
public class MenuManager : MonoBehaviour
{
public MenuManager instance;
public string CurrentMenu;
public string MatchName = "";
public string MatchPassword = "";
public int MatchMaxUsers = 32;
private Vector2 ScrollLobby = Vector2.zero;
void Start()
{
CurrentMenu = "Main";
MatchName = "DefaultMatchName " + Random.Range(0, 5000);
instance = this;
}
void FixedUpdate()
{
instance = this;
}
void OnGUI()
{
if(CurrentMenu == "Main")
MenuMain();
if(CurrentMenu == "Lobby")
MenuLobby();
if(CurrentMenu == "HostGame")
MenuHostGame();
if(CurrentMenu == "ChooseGame")
MenuChooseMap();
}
public void NavigateTo(string nextmenu)
{
CurrentMenu = nextmenu;
}
private void MenuMain()
{
if(GUI.Button(new Rect(10,10,200,50), "Host Game"))
{
NavigateTo("HostGame");
}
if(GUI.Button(new Rect(10,70,200,50), "Refresh Servers"))
{
MasterServer.RequestHostList("DeathMatch");
}
GUI.Label (new Rect(220, 10, 130, 30), "Player Name:");
MultiplayerManager.instance.PlayerName = GUI.TextField (new Rect(400, 10, 200, 30), MultiplayerManager.instance.PlayerName);
if(GUI.Button(new Rect(620,10,100,30), "Save Name"))
{
PlayerPrefs.SetString ("PlayerName", MultiplayerManager.instance.PlayerName);
}
GUILayout.BeginArea(new Rect(Screen.width -400, 0, 400, Screen.height), "Server List", "Box");
GUILayout.Space (20);
foreach(HostData match in MasterServer.PollHostList())
{
GUILayout.BeginHorizontal("Box");
GUILayout.Label (match.gameName);
if(GUILayout.Button ("Connect"))
{
Network.Connect (match);
}
GUILayout.EndArea ();
}
GUILayout.EndArea();
}
private void MenuHostGame()
{
if(GUI.Button(new Rect(10,10,200,50), "Back"))
{
NavigateTo("Main");
}
if(GUI.Button(new Rect(10,60,200,50), "Start Server"))
{
MultiplayerManager.instance.StartServer (MatchName, MatchPassword, MatchMaxUsers);
}
if(GUI.Button(new Rect(10,160,200,50), "Choose Map"))
{
NavigateTo ("ChooseGame");
}
GUI.Label (new Rect(220, 10, 130, 30), "Match Name");
MatchName = GUI.TextField (new Rect(400, 10, 200, 30), MatchName);
GUI.Label (new Rect(220, 60, 130, 30), "Match Password");
MatchPassword = GUI.PasswordField (new Rect(400, 60, 200, 30), MatchPassword, '*');
GUI.Label (new Rect(220, 100, 130, 30), "Match Max Players");
GUI.Label (new Rect(400, 100, 200, 30), MatchMaxUsers.ToString());
MatchMaxUsers = Mathf.Clamp (MatchMaxUsers, 8, 32);
if(GUI.Button(new Rect(425,100,25,30), "+"))
MatchMaxUsers += 2;
if(GUI.Button(new Rect(450,100,25,30), "-"))
MatchMaxUsers -= 2;
GUI.Label (new Rect(650, 10, 130, 30), MultiplayerManager.instance.CurrentMap.MapName);
}
private void MenuLobby()
{
GUILayout.BeginScrollView(ScrollLobby, GUILayout.MaxWidth(200));
foreach(MPPlayer pl in MultiplayerManager.instance.PlayerList)
{
GUILayout.Box(pl.PlayerName);
}
GUILayout.EndScrollView ();
GUI.Box (new Rect(250, 10, 200, 40), MultiplayerManager.instance.CurrentMap.MapName);
if(Network.isServer)
{
if(GUI.Button (new Rect(Screen.width - 200, Screen.height - 80, 200, 40), "Start Game"))
{
MultiplayerManager.instance.networkView.RPC ("Client_LoadMultiplayerMap", RPCMode.AllBuffered, 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"))
{
Network.Disconnect();
}
}
private void MenuChooseMap()
{
if(GUI.Button(new Rect(10,10,200,50), "Back"))
{
NavigateTo("HostGame");
}
GUI.Label (new Rect(220, 10, 150, 50), "Choose Map");
GUILayout.BeginArea (new Rect(350, 10, 150, Screen.height));
foreach(MapSetting map in MultiplayerManager.instance.MapList)
{
if(GUILayout.Button (map.MapName))
{
NavigateTo ("HostGame");
MultiplayerManager.instance.CurrentMap = map;
}
}
GUILayout.EndArea();
}
void OnServerInitialized()
{
NavigateTo ("Lobby");
}
void OnConnectedToServer()
{
NavigateTo ("Lobby");
}
void OnDisconnectedFromServer(NetworkDisconnection info)
{
NavigateTo ("Main");
}
}
Thanks in advance for any help
~ Sniper