I have a prefab player which i instantiate when connecting as a host or client. Within my prefab hierarchy, I have a cube and another mesh that I create dynamically.
When instanciating my prefab with Instantiate then calling NetworkServer.AddPlayerForConnection. Only my cube is ready to be rendered inside the network but my mesh is not because the generation is still in progress.
So how can i spawn it when the generation is finished?
Can you wait to call AddPlayerForConnection until after your mesh is generated? I’m not familiar with how a mesh is generated, but if there is a callback, you could trigger AddPlayerForConnection from there?
Before the function call AddPlayerForConnection, I call only Instantiate(myPrefab). So how can I wait ? I have try a infinite loop and register an event handler but i never reach the call AddPlayerForConnection.
Using infinite loops for waiting is a bad idea, since it stops any thread execution beyond current code path. Have you tried using Coroutines? Try something like:
private WaitForSeconds _delay = new WaitForSeconds(1f);
...
// ... before your AddPlayerForConnection call ...
StartCoroutine(WaitForGeneration());
...
private IEnumerator WaitForGeneration(...){
while (!generationFinished){
yield return _delay;
}
// Add Player here
// Instantiate(...);
AddPlayerForConnection(...);
yield return null;
}
The solution that I used was to let all unity stuff connection go as usual and use coroutine like you said at the player local and update it for each client when something changes. It works but maybe not efficient as like you suggested above.