hi, i have a problem with my networking code, and im unable to solve it.
im getting this error:
NullReferenceException: Object reference not set to an instance of an object
MultiPlayerManager.Client_SpawnPlayer (NetworkPlayer player, Vector3 position, Quaternion rotation) (at Assets/Custom scripts/MultiPlayerManager.cs:184)
UnityEngine.NetworkView:RPC(String, RPCMode, Object[ ])
MultiPlayerManager:Server_SpawnPlayer(NetworkPlayer) (at Assets/Custom scripts/MultiPlayerManager.cs:177)
MultiPlayerManager:SpawnMenu() (at Assets/Custom scripts/MultiPlayerManager.cs:227)
MultiPlayerManager:OnGUI() (at Assets/Custom scripts/MultiPlayerManager.cs:167)
heres the multiplayer code:
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 = 31;
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 ("Player Name");
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(maxusers, 25002, !Network.HavePublicAddress());
MasterServer.RegisterHost ("Command and Vanquish", MatchName, "");
Debug.Log("Master Server Info:" + MasterServer.ipAddress +":"+ MasterServer.port);
Debug.Log("initializing server");
}
void OnServerInitalized()
{
Server_PlayerJoinRequest (PlayerName, Network.player);
Debug.Log("Server initialized and ready");
}
void OnConnectedToServer()
{
networkView.RPC ("Server_PlayerJoinRequest", RPCMode.Server, PlayerName, Network.player);
Debug.Log("connected to Server");
}
void OnPlayerDisconnected(NetworkPlayer id)
{
networkView.RPC ("Client_RemovePlayer", RPCMode.All, id);
Debug.Log("Player disconnected");
}
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, "", "");
Debug.Log("Player connected");
}
void OnDisconnectedFromServer()
{
PlayerList.Clear();
Debug.Log("list cleared");
}
[RPC]
void Server_PlayerJoinRequest(string playername, NetworkPlayer view)
{
networkView.RPC ("Client_AddPlayerToList", RPCMode.All, playername, view);
Debug.Log("rpc server Player join request sent");
}
[RPC]
void Client_AddPlayerToList(string playername, NetworkPlayer view)
{
MPPlayer tempplayer = new MPPlayer ();
tempplayer.PlayerName = playername;
tempplayer.PlayerNetwork = view;
PlayerList.Add(tempplayer);
Debug.Log("rpc client Player added to list");
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);
}
Debug.Log("rpc client removed player");
}
[RPC]
void Client_GetMultiplayerMatchSettings(string map, string mode, string others)
{
CurrentMap = GetMap (map);
Debug.Log("rpc client multiplayer match settings gotten");
}
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);
Debug.Log("rpc client load multiplayer map working");
}
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)
{
MatchLoaded = true;
Spawnpoints = GameObject.FindGameObjectsWithTag("spawnpoint");
networkView.RPC("Client_ServerLoaded", RPCMode.AllBuffered, IsMatchStarted);
}
}
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("Server_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 PlayerScore;
public int PlayerKills;
public int PlayerDeahts;
}
[System.Serializable]
public class MapSetting
{
public string MapName;
public string MapLoadName;
public Texture MapLoadTexture;
}
and the playermanager code:
using UnityEngine;
using System.Collections;
public class PlayerManager : MonoBehaviour
{
public MPPlayer thisplayer;
public PlayerController Controller;
public Transform ControllerTransform;
public GameObject OutsideView;
public Transform CurrentPosition;
public Quaternion CurrentRotation;
void Start()
{
ControllerTransform.gameObject.SetActive(false);
thisplayer.PlayerManager = this;
thisplayer = MultiPlayerManager.GetMPPlayer(networkView.owner);
OutsideView.SetActive (false);
}
void FixedUpdate ()
{
if(networkView.isMine)
{
CurrentPosition.position = ControllerTransform.position;
CurrentRotation = ControllerTransform.rotation;
}
else
{
OutsideView.transform.position = CurrentPosition.position + new Vector3(0,-1f,0);
OutsideView.transform.rotation = CurrentRotation;
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 position = CurrentPosition.position;
if (stream.isWriting)
{
stream.Serialize(ref position);
stream.Serialize(ref CurrentRotation);
}
else
{
stream.Serialize(ref position);
stream.Serialize(ref CurrentRotation);
}
}
void HandleBulletDamage (int damage)
{
if (Network.isServer)
{
Server_HandleBulletDamage (damage);
}
else
{
networkView.RPC ("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.SetActive(false);
if (networkView.isMine)
{
ControllerTransform.gameObject.SetActive(false);
}
}
[RPC]
public void Client_PlayerAlive()
{
if (networkView.isMine)
{
ControllerTransform.gameObject.SetActive(true);
}
else
{
OutsideView.SetActive(true);
}
}
}
so far im not sure whats causing this, im having the problem that my name doesnt appear on the playerlist though if im the guy hosting. id really like to hear what you guys think as ive been on this issue for several hours, without success.