I have a networked project utilizing UNet and running into some odd problems when changing scenes.
If the client tries to spawn too quickly after the scene change the state of network ids on the various objects is not in synch and terrible things ensue.
I have found that if I set a time delay of a certain amount (3 seconds currently) this seems to allow the client to receive the state of objects and then spawn and all works ok.
But there MUST be some way to know exactly when this happens and not have to wait some arbitrary time that will surely fail in many cases anyway and cause clients to wait longer than necessary in others.
public override void OnClientSceneChanged( NetworkConnection conn )
{
clientConnection = conn;
clientSpawnTime = Time.time + 3.0f; // FIXME : WTF?! There must be a more reliable way to determine when the client has recieved the state of the scene objects from the server before spawning
}
public void UpdateClientSpawn()
{
// Clients wait to see if the scene is going to change before spawning
if ( clientConnection == null )
return;
if ( ( clientSpawnTime <= 0.0f ) || ( Time.time < clientSpawnTime ) )
return;
if ( connectState != ConnectState.MatchClient )
return;
if ( ( networkSceneName == "" ) || ( networkSceneName.StripPath() == SceneManager.GetActiveScene().name.StripPath() ) )
{
ClientSpawnPlayer( clientConnection );
clientSpawnTime = 0.0f;
}
else
{
clientSpawnTime = Time.time + 1.0f;
}
}