From what I’ve read, to use a custom player object you simply need to override the OnServerAddPlayer() in your network manager. As long as [Auto Create Player ], most of the Object Creation Flow should be taken care of.
using UnityEngine;
using UnityEngine.Networking;
public class MyNetworkManager : NetworkManager {
private GameObject playerBall; //prefab (registered on MyNetworkManager GUI component)
public void ChooseBall(GameObject selectedBall){ //Button onClick passes in our player prefab
playerBall = selectedBall; //select our player prefab
Application.LoadLevel("FightScene"); //load next level [Don't Destroy On Load ☑]
}
void OnLevelWasLoaded(int level){ //when next level is loaded...
if (level == 1) GetComponent<NetworkManagerHUD>().showGUI = true; //...show network HUD
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
Transform startingPosition = GetStartPosition(); //Finds a spawn position based on NetworkStartPosition objects in the scene, used by the default implementation of OnServerAddPlayer.
GameObject player = (GameObject)Instantiate(playerBall, startingPosition.position, startingPosition.rotation);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
What happens: I select Host, playerBall is spawned. In another instance of the game I join as client, playerBall is spawned on the server. Host sees 2 playerBalls, Client sees none.
Failed to spawn server object, assetId=52db34a96abef44bb66c11050d21c39 netId=4
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
My Question: Why are the playerBall’s not showing up on the client? How should I debug something like this?
For anyone kind enough to reply, Thanks in advance!
On a side note
Once in a great while they both spawn correctly, and I can control both of them from their respective game instances. It is how ever very laggy. Most of the time the client simply freezes up then stops. I also occasionally get the following errors:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkScene.RegisterPrefab (UnityEngine.GameObject prefab) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkScene.cs:108)
UnityEngine.Networking.ClientScene.RegisterPrefab (UnityEngine.GameObject prefab) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:336)
MyNetworkManager.Start () (at Assets/Title/MyNetworkManager.cs:8)
UNet Server Disconnect Error: BadMessage conn:[hostId: 0 connectionId: 1 isReady: True channel count: 2]:1
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()