- Home /
Can't see the host of the server
I got this problem where I can as a host see all the players connected to the server, but the other players can't see the host. It did work in the start of the project but not anymore. And I just can't find it. I've tried numerous ways without any success. I hope you guys can help me.
Edit: I've narrowed it down to that the hosts Outsideview don't get activated for some reason.
My multiplayer manager that has all the info for connection, disconnection, spawning and so forth
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiplayerManager : MonoBehaviour {
public static MultiplayerManager instance;
public GUIManager guiManager;
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_GetMultiplayerMatchSettings",player,CurrentMap.MapName,"","");
}
void OnDisconnectedFromServer(){
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);
if(Network.player == view){
MyPlayer = tempplayer;
GameObject play = Network.Instantiate(PlayerManagerPrefab,Vector3.zero,Quaternion.identity,5) as GameObject;
}
}
[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);
}
[RPC]
void Server_SpawnPlayer(NetworkPlayer player){
int numberspawn = Random.Range(0,Spawnpoints.Length - 1);
MyPlayer.PlayerManager.OutsideView.SetActive(true);
networkView.RPC("Client_SpawnPlayer",RPCMode.All,player, Spawnpoints[numberspawn].transform.position + new Vector3(0,2,0),Spawnpoints[numberspawn].transform.rotation);
}
[RPC]
public 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{
}
}
[RPC]
public void Client_PlayerAlive(){
PlayerManager pm = PlayerManagerPrefab.GetComponent<PlayerManager>();
pm.Client_PlayerAlive();
}
void OnLevelWasLoaded(){
if(Application.loadedLevelName == CurrentMap.MapLoadName && Network.isServer){
MatchLoaded = true;
Spawnpoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
networkView.RPC("Client_ServerLoaded",RPCMode.AllBuffered, IsMatchStarted);
}
}
[RPC]
void Client_ServerLoaded(bool started){
MatchLoaded = true;
IsMatchStarted = started;
}
public static MPPlayer GetMPPlayer(NetworkPlayer player){
foreach(MPPlayer play in MultiplayerManager.instance.PlayerList){
if(play.PlayerNetwork == player){
return play;
}
}
return null;
}
void OnGUI(){
if(!MyPlayer.PlayerIsAlive && IsMatchStarted){
SpawnMenu();
}
if(IsMatchStarted && Input.GetKey(KeyCode.Tab)){
guiManager.ScoreBoard();
}
}
//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("Server_SpawnPlayer",RPCMode.Server,Network.player);
}
}
[RPC]
public void Client_UpdatePlayerHealthAndAlive(NetworkPlayer targetplayer, bool IsAlive, int currenthealth){
MPPlayer player = MultiplayerManager.GetMPPlayer(targetplayer);
player.PlayerIsAlive = IsAlive;
player.PlayerHealth = currenthealth;
}
}
[System.Serializable]
public class MPPlayer{
public string PlayerName = "";
public NetworkPlayer PlayerNetwork;
public PlayerManager PlayerManager;
public int PlayerHealth = 100;
public bool PlayerIsAlive;
public int PlayerScore;
public int PlayerKills;
public int PlayerDeaths;
}
[System.Serializable]
public class MapSetting{
public string MapName;
public string MapLoadName;
public Texture MapLoadTexture;
}
My PlayerManager that has the info for the world model and so forth
using UnityEngine;
using System.Collections;
public class PlayerManager : MonoBehaviour {
public MPPlayer thisplayer
{
get{
return MultiplayerManager.GetMPPlayer(networkView.owner);
}
}
public PlayerController Controller;
public Transform ControllerTransform;
public GameObject OutsideView;
public Vector3 CurrentPosition;
public Quaternion CurrentRotation;
void Start(){
DontDestroyOnLoad(gameObject);
ControllerTransform.gameObject.SetActive(false);
OutsideView.SetActive(false);
thisplayer.PlayerManager = this;
}
void FixedUpdate(){
if(networkView.isMine){
CurrentPosition = ControllerTransform.position;
CurrentRotation = ControllerTransform.rotation;
}
else{
OutsideView.transform.position = CurrentPosition + new Vector3(0,-1f,0);
OutsideView.transform.rotation = CurrentRotation;
}
}
void OnSerializeNetworkView(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 GetBulletDamage(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){ //When dead
thisplayer.PlayerIsAlive = false;
thisplayer.PlayerHealth = 0;
thisplayer.PlayerDeaths++;
networkView.RPC("Client_PlayerDead",RPCMode.All);
MultiplayerManager.instance.networkView.RPC("Client_UpdatePlayerHealthAndAlive",RPCMode.Others,thisplayer.PlayerNetwork, false,0);
}
else{ //Getting hit while alive
MultiplayerManager.instance.networkView.RPC("Client_UpdatePlayerHealthAndAlive",RPCMode.All,thisplayer.PlayerNetwork,thisplayer.PlayerIsAlive,thisplayer.PlayerHealth);
}
}
[RPC]
public void Client_PlayerDead(){
OutsideView.SetActive(false);
if(networkView.isMine)
ControllerTransform.gameObject.SetActive(false);
}
[RPC]
public void Client_PlayerAlive(){
OutsideView.SetActive(true);
if(networkView.isMine)
ControllerTransform.gameObject.SetActive(true);
else
OutsideView.SetActive(true);
}
}
I really hope you guys can help. Thanks in advance
Answer by Bunny83 · Feb 20, 2013 at 04:58 PM
I didn't dig through your whole code, but i guess that the server doesn't have it's own player in his local player list. Usually the server can't send an RPC to himself.
So i would suggest to handle the server player differently since he's always in the list from the very beginning to the end.
So this is the part that should be changed:
void OnServerInitialized()
{
// add server player to local list, maybe by calling "Client_AddPlayerToList" manually
}
Didn't get that work. I either couldn't spawn or it was the same as before.
I'll post my edit here as I posted in the original question. "Edit: I've narrowed it down to that the hosts Outsideview don't get activated for some reason."
Your answer
![](https://koobas.hobune.stream/wayback/20220613093048im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
How to get the Host's player position ? 0 Answers
Network messages (OnPlayerConnected, OnServerInitialized) never gets called 0 Answers
How to add and remove objects from a list when they get destroyed and instantiated 0 Answers
Requesting data from server 1 Answer