UNET : random bugs with spawning
Hello everyone I'm asking for help on this issue I have as I can't understand what's happening at all.
I'm working on a networked game with 2 players. First they connect in a scene, and then the host player starts the game, and we change scene to the one where everything is happening. When switching scene (calling OnSceneWasSwitched) I send a command to create two different gameobjects : one for player 1, one for player 2 and spawn them with authority.
private void OnSceneWasSwitched(Scene previousScene, Scene newScene)
{
if (hasAuthority) CmdInitPlayer();
}
[Command]
private void CmdInitPlayer()
{
GameManager.Instance.InitGamePlayer(PlayerId);
}
public void InitGamePlayer(int playerId)
{
switch (playerId)
{
case 1:
CreateGod();
break;
case 2:
CreateTowerPlayer();
break;
default:
break;
}
}
public void CreateGod()
{
God = GameObject.FindGameObjectWithTag(Tags.God) == null
? (GameObject) Instantiate(godPrefab, Vector3.zero, Quaternion.identity)
: GameObject.FindGameObjectWithTag(Tags.God);
NetworkServer.SpawnWithClientAuthority(God, NetworkServer.connections[0]);
}
public void CreateTowerPlayer()
{
TPlayer = GameObject.FindGameObjectWithTag(Tags.TPlayer) == null
? (GameObject) Instantiate(tPlayerPrefab, Vector3.zero, Quaternion.identity)
: GameObject.FindGameObjectWithTag(Tags.TPlayer);
NetworkServer.SpawnWithClientAuthority(TPlayer, NetworkServer.connections[1]);
}
All of this works perfectly well until spawning these two gameobjects : both are instantiated on server, but only the first one is spawning over the network. No warning, no error. As the game goes on, other games objects can be instantiated and spawned perfectly well, with or without authority.
As I can't understand what's happening, I tried dumb tests, that went horribly wrong : I tried adding registerprefab right before instantiating it. Nothing. Then I tried something really dumb : inverting authority for the two gameobjects. Both still appear on server, and the one that spawns is inverted. So the problem must come only from connection 1, but I really can't understand why. Especially when a later spawn with this connection is working.
My main issue is probably not having any warning nor error!
I'd be so thankful if someone could at least give me some ideas.
Your answer
![](https://koobas.hobune.stream/wayback/20220612111051im_/https://answers.unity.com/themes/thub/images/avi.jpg)