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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Custom Spawn Functions - Pooling issue

Discussion in 'Multiplayer' started by Doghelmer, Aug 25, 2018.

  1. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    120
    I have an issue at the moment that's driving me a bit nuts. I'm using the UNet Custom Spawns system detailed on this page to do object pooling on both the client and server.

    I'm registering the spawn handler to spawn items, as indicated on this page:
    itemAssetID = gc.spawnerMain.itemPrefab.GetComponent<NetworkIdentity>().assetId;
    ClientScene.RegisterSpawnHandler(itemAssetID, SpawnItemClient, UnSpawnItemClient);

    However, sometimes when I spawn an item on the server using NetworkServer.Spawn(), SpawnItemClient is called MULTIPLE times in succession. First, the item I want is spawned. Then, a couple of other items from the pool that had previously been spawned -- and subsequently unspawned using NetworkServer.UnSpawn() -- are spawned again.

    Any ideas on how a single NetworkServer.Spawn() call could possibly result in multiple spawns on the client like this? Is it possible that multiple items are somehow getting registered as one?
     
    Last edited: Aug 25, 2018
  2. Doghelmer

    Doghelmer

    Joined:
    Aug 30, 2014
    Posts:
    120
    Found the source of the issue. It's a bit complicated, but for anyone else who might run into this, check to see that you're not calling NetworkIdentity.RebuildObservers() inappropriately
     
unityunity