Search Unity

Creating a part of a player which is not include within the prefab

Discussion in 'Multiplayer' started by aralambomahay, Jun 23, 2017.

  1. aralambomahay

    aralambomahay

    Joined:
    Apr 12, 2017
    Posts:
    10
    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?
     
  2. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    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?
     
  3. aralambomahay

    aralambomahay

    Joined:
    Apr 12, 2017
    Posts:
    10
    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.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    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:
    Code (CSharp):
    1. private WaitForSeconds _delay = new WaitForSeconds(1f);
    2.  
    3. ...
    4.  
    5. // ... before your AddPlayerForConnection call ...
    6. StartCoroutine(WaitForGeneration());
    7.  
    8. ...
    9.  
    10. private IEnumerator WaitForGeneration(...){
    11.    while (!generationFinished){
    12.        yield return _delay;
    13.    }
    14.    // Add Player here
    15.    // Instantiate(...);
    16.    AddPlayerForConnection(...);
    17.    yield return null;
    18. }
     
  5. aralambomahay

    aralambomahay

    Joined:
    Apr 12, 2017
    Posts:
    10
    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.