[Resolved With Palliative]NetworkManager.OnServerAddPlayer is not called from ClientScene.AddPlayer

Hi guys,

This issue is killing me. I`ll explain the full cenario:

Im using MatchMaker to create, list and join matches. Nothings wrong with that, and I also have my custom NetworkManager with autoCreatePlayer checked, my playerPrefab, online and offline scenes set.

My online scene have scripts that needs player`s references. But the player not already exists in online scene, so what I did was:

In my custom NetworkManager I overrided OnServerAddPlayer method, and there I resolved all dependencies that needed the player instance, before calling NetworkServer.AddPlayerForConnection, and that worked, but ONLY for host player.

When another player connects as a client, the method OnClientConnect is called, but when I call ClientScene.AddPlayer(conn, 0) from there, no OnServerAddPlayer method is called for the client. It seens like AddPlayer Message is not being sent, and I don`t know why.

Online documentation tells that every ClientScene.AddPlayer will call OnServerAddPlayer. I already tried not to overried OnClientConnect method, but nothings changeded.

What can be wrong? There is my custom NetworkManager code:

public override void OnClientConnect(NetworkConnection conn)
	{
		Debug.Log("Entered OnClientConnect");
		ClientScene.AddPlayer(conn, 0);
	}

	public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
	{
		Debug.Log("Entered OnServerAddPlayer");
		var player = (GameObject)GameObject.Instantiate(playerPrefab, GetStartPosition().position, Quaternion.identity);
		if(NetworkManager.singleton.numPlayers > 0){
			player.tag = "Default";
		}else{
			player.tag = "Player";
		}
		if(NetworkServer.AddPlayerForConnection(conn, player, playerControllerId)){
			player.GetComponent<PlayerNetworkSetup>().SetUpPlayerDependencies(player.GetComponent<Player>());
		}

	}

Thanks for helping, and sorry for my bad English.

Hi guys,

I resolved this problem with a different sollution.

Instead of overriding OnServerAddPlayer method, I’ve created a new script that extends from NetwrokBehavior and attached it to the player.

In that script I overrided OnStartLocalPlayer.

So every new local player created will call this method and there I resolved all player’s prefab dependencies ;).

1 Like