- Home /
Multiplayer different player prefab
Hi there I have build a multiplayer game where a player can choose his/her character he/she wants to be. If I play the game on my pc (open up two versions of the game) it is working perfectly, but when I run it on different pc's the host selection prefab is the same for everyone else (clients). I don't know what the problem.. is it to do with when the host start the game is says "client address = localhost", but on the client side it says "client address = 192.168.0.3"... Please help
Here is the code of the Network Manager
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class netmanagercustom : NetworkManager {
public class MsgTypes
{
public const short PlayerPrefab = MsgType.Highest + 1;
public class PlayerPrefabMsg : MessageBase
{
public short controllerID;
public short prefabIndex;
}
}
public class NetworkController : NetworkManager
{
[SerializeField] Vector3 playerSpawnPos;
[SerializeField] GameObject character1;
[SerializeField] GameObject character2;
[SerializeField] GameObject character3;
[SerializeField] GameObject character4;
[SerializeField] GameObject character5;
[SerializeField] GameObject character6;
[SerializeField] GameObject character7;
[SerializeField] GameObject character8;
[SerializeField] GameObject character9;
[SerializeField] GameObject character10;
[SerializeField] GameObject character11;
GameObject chosenCharacter;
public short playerPrefabIndex;
public override void OnStartServer()
{
NetworkServer.RegisterHandler(MsgTypes.PlayerPrefab, OnResponsePrefab);
base.OnStartServer();
}
public override void OnClientConnect(NetworkConnection conn)
{
client.RegisterHandler(MsgTypes.PlayerPrefab, OnRequestPrefab);
base.OnClientConnect(conn);
}
private void OnRequestPrefab(NetworkMessage netMsg)
{
MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg();
msg.controllerID = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>().controllerID;
msg.prefabIndex = playerPrefabIndex;
client.Send(MsgTypes.PlayerPrefab, msg);
}
private void OnResponsePrefab(NetworkMessage netMsg)
{
MsgTypes.PlayerPrefabMsg msg = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>();
playerPrefab = spawnPrefabs[msg.prefabIndex];
base.OnServerAddPlayer(netMsg.conn, msg.controllerID);
Debug.Log(playerPrefab.name + " spawned!");
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg();
msg.controllerID = playerControllerId;
NetworkServer.SendToClient(conn.connectionId, MsgTypes.PlayerPrefab, msg);
//var player = (GameObject)GameObject.Instantiate(chosenCharacter, playerSpawnPos, Quaternion.identity);
//NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
[SerializeField] Vector3 playerSpawnPos;
[SerializeField] GameObject character1;
[SerializeField] GameObject character2;
[SerializeField] GameObject character3;
[SerializeField] GameObject character4;
[SerializeField] GameObject character5;
[SerializeField] GameObject character6;
[SerializeField] GameObject character7;
[SerializeField] GameObject character8;
[SerializeField] GameObject character9;
[SerializeField] GameObject character10;
[SerializeField] GameObject character11;
GameObject chosenCharacter;
void Update()
{
if (PlayerPrefs.GetInt ("PlayerNumber") == 0) {
chosenCharacter = character1;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 1) {
chosenCharacter = character2;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 2) {
chosenCharacter = character3;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 3) {
chosenCharacter = character4;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 4) {
chosenCharacter = character5;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 5) {
chosenCharacter = character6;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 6) {
chosenCharacter = character7;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 7) {
chosenCharacter = character8;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 8) {
chosenCharacter = character9;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 9) {
chosenCharacter = character10;
}
if (PlayerPrefs.GetInt ("PlayerNumber") == 10) {
chosenCharacter = character11;
}
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId){
var player = (GameObject)GameObject.Instantiate (chosenCharacter, playerSpawnPos, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
Ok, I have check with a few Debug.Logs what is going on. The "PlayerPrefs.SetInt" does work and it sees the correct number over the network, but the problem is that it is not spawning the correct Character associated with the specific "PlayerPrefs" counter.
Ok i got it right, Finally... If you also have this problem, here is how to fix it.. link text