Hello, this is my first post so be gentle.
After using the auto-spawn for the player i wanted to create a function for setting up player objects manually.
After navigating through the menu and pressed eighter host or connect, the game will in general set up an connection and change the game scene to the one used for the actual game. When the scene is loaded ClientScene.Ready and ClientScene.AddPlayer are run (unless the game is running as a standalone server), as you can see in my codesnippets under.
When connecting as a host the game scene is loaded and my player is spawned and is under my controll. If someone else would try to connect to the server, they will be able to connect to the server and the game scene is loaded. But no player will spawn (not the previous connected players eighter) and i get this error message:
Failed to spawn server object, assetId=1aee37fb8296127491a72b1d2b366e netId=1
I get one for each player object that has been tried to be spawned. In other words, player spawning works for hosts but not clients.
I first tought this was because the player prefab had not been registered, but i’ve tried with the ClientScene.RegisterPrefab and dragging the prefab into the NetworkManager. I also tried using both at one, and none. And after two hours of googling and forum browsing i still have not been able to figure it out. It might just be a dumb way to set up player objects (im still getting used to the new networking).
Any help is appreciated, and also if you know better ways to set up player objects (no auto spawn) i would be very gratefull. Thanks!
public class CustomNetworkManager : NetworkManager {
public NetworkClient myclient;
public NetworkConnection myconnectiontoserver;
public bool runningashost = false;
void Awake()
{
DontDestroyOnLoad(this);
singleton = this;
}
void Start()
{
NetworkServer.RegisterHandler(MsgType.AddPlayer, OnClientAddPlayer);
}
void OnLevelWasLoaded(int lvl)
{
if (lvl == 1) {
NetworkServer.SpawnObjects();
ClientScene.Ready(myconnectiontoserver);
ClientScene.AddPlayer(0);
}
}
public void OnClientConnect(NetworkMessage netmsg)
{
myconnectiontoserver = netmsg.conn;
if (!runningashost)
{
Application.LoadLevelAsync(1);
}
}
public void OnClientAddPlayer(NetworkMessage netmsg)
{
GameObject playerobj = (GameObject)Instantiate(Resources.Load("Prefabs/Player/Player"),new Vector3(400,20,400),Quaternion.identity) as GameObject;
ClientScene.RegisterPrefab(playerobj);
NetworkServer.AddPlayerForConnection(netmsg.conn, playerobj, 0);
}
public void connectAsClient(string ip, int port)
{
myclient = new NetworkClient();
myclient.RegisterHandler(MsgType.Connect, OnClientConnect);
myclient.Connect(ip, port);
}
public void startAsHost(int port)
{
runningashost = true;
startAsServer(port);
setUpLocalClient();
}
public void startAsServer(int port)
{
this.networkPort = port;
NetworkServer.Listen(port);
NetworkManager.singleton.ServerChangeScene("WorldScene");
}
public void setUpLocalClient()
{
myclient = ClientScene.ConnectLocalServer();
myclient.RegisterHandler(MsgType.Connect, OnClientConnect);
}
}
All the code is within a CustomNetworkManager class which inherits the NetworkManager.