Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Freezing with too much Spawn call

Discussion in 'Multiplayer' started by panicq, Jul 11, 2015.

  1. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    Hello,

    I'm doing a multiplayer game with the new UNet API, and as I said in the title sometimes my game freezes when I've too many instances on the network. Before spawing my objects I need to check if they are not colliding with trees, or players, so I need another loop. And I cannot log anything in the console....

    Code (CSharp):
    1. void SpawnBonus()
    2.     {
    3.         for(int i = 0; i < 50; i++)
    4.         {
    5.             SpawnABonus ();
    6.         }
    7.     }
    8. public void SpawnABonus()
    9. {
    10. Vector3randomPosition = newVector3(Random.Range(-50.0f,50.0f),0.32f, Random.Range(-50.0f,50.0f));
    11. bool placementOk = false;
    12.  
    13. while(!placementOk)
    14. {
    15.      //When it freezes I can't the log is not displayed in the console:
    16.      Debug.Log("Try to spawn");
    17.  
    18.      Collider[] hitColliders = Physics.OverlapSphere(randomPosition, 1);
    19.  
    20.     foreach(Collider collider in hitColliders)
    21.     {
    22.         if(collider.tag == "Tree" || collider.tag == "Player" || collider.tag == "Bonus")
    23.     {
    24.     randomPosition = newVector3(Random.Range(-50.0f,50.0f),0.32f,  Random.Range(-50.0f,50.0f));
    25.      Debug.Log("In a tree or player");
    26.      break;
    27. }
    28.  
    29.      placementOk = true;
    30.   }
    31. }
    32.  
    33.     GameObject bonus = Instantiate(bonusPrefab, randomPosition, Quaternion.identity) as GameObject;
    34.     NetworkServer.Spawn(bonus);
    35. }
    36.  
    PS: Sorry for bad indentation, it didn't paste correctly :/

    Any ideas ?

    Thank you in advance :)
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Fairly grotesque brute force code here.

    Try not to spawn all in same frame, instead simply add what needs to be spawned to a queue and it will check each frame a little more. Since in a typical game you get 30 - 60 opportunities per second to check, it is ok to spawn within a second.

    Another thing, you shouldn't really be doing a bunch of different string compares within while and for loops... its a recipe for absolute disaster.

    Also any debug logs will be horrifically slow too.

    Really, this whole pattern needs to be thrown in the bin and redone from scratch.
     
  3. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    So you would use a coroutine ?
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    No, I'd just do one progressive check each frame. Why would I need to lose control with a co-routine? I think it would be better if you explained clearly the task you want to perform, because this isn't actually networking related, but a problem with brute forcing and nested loops.
     
  5. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    Okay, I just need to spawn 50 bonus prefab when the host starts the game. But I need to check if they don't spawn inside trees or players.
     
  6. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    put a counter in the inner loop to see how many times it fails to find an empty space.. sounds like it is failing a lot.
     
    panicq likes this.
  7. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    I've put a log in the while loop, and ran it ten times, it fails to find an empty space from 2, up to 12 times for 50 instances.