SpawnHandler from NetworkClient.RegisterPrefab(prefab, SpawnHandler, UnspawnHandler); never gets called on client
Describe the bug SpawnHandler from NetworkClient.RegisterPrefab(prefab, SpawnHandler, UnspawnHandler); never gets called on client (Also the docs are out of date; ClientScene.RegisterPrefab is obsolete)
How to reproduce the issue, step by step Copy the code for Setting Up a Game Object Pool with Custom Spawn Handlers in Mirror Docs here: https://mirror-networking.gitbook.io/docs/guides/gameobjects/custom-spawnfunctions#setting-up-a-game-object-pool-with-custom-spawn-handlers
- Build or test multiplayer in the editor.
- Spawn some objects on the host.
- Unspawn some objects on the host.
- Spawn the some more on the host.
Notice that the host is using the object pool but the client is not. Client instantiates new prefabs instead of using the pooled ones. Unspawned client objects remain in the scene and is not reused. The SpawnHandler function on the client never gets called, instead a new prefab is instatiated on the client.
Expected behavior Both client and host will use the object pool. SpawnHandler gets called on the client when the host spawns an object.
Screenshots
On the left you can see a small number of pooled coins on the host
On the right you can see a long list of unpooled coins being generated on the client
Desktop (please complete the following information):
- OS: windows 10
- Build target: windows 64
- Unity version: 2018.4.34f1
- Mirror branch: Latest Unity Asset store version
SpawnHandler never gets called on the client
UnSpawn handler does get called on the client
@MrGadget1024 or anyone who ever used spawn handlers: any thoughts?
@MrGadget1024 or anyone who ever used spawn handlers: any thoughts?
I have a test case for this but it requires #2797 to be merged for it to work.
thanks for bug report, gonna take a look
@spinaljack any chance you could share the exact code that you used?
going through our pool example. the script has some compilation errors, need to update the example. probably easier to showcase with our tanks example too.
noticed another issue when experimenting. build=server-only editor=client fire a few times build=stop server, then press start host editor=client
then fire again. now gets this error.
looks like something isn't cleared when stopping.

when starting for the first time (not like the above error), all works fine though.
build=host editor=client
firing uses the pool. which means that unspawn handler is called: https://gyazo.com/0dce45254a45fb28497c499f3fe414f2
the only way to reproduce weird behaviour is when using NetworkClient.RegisterPrefab for a prefab that is still in NetworkManager's prefabs.
BUT this will show a big warning when attempting to do so:

in our PrefabPool example, this would keep prrojectiles in the scene forever.
see branch: repro_2705
@spinaljack is right, if prefab is registered twice (once in networkmanager prefabs, once with NetworkClient.RegisterPrefab) then it's spawned regularly, but it's despawned via unspawn handler.
there's a big warning explaining not to register twice, which is why this happens. but perhaps we should make this consistent anyway.
SpawnPrefab looks for prefab first instead of looking for spawn handlers first:

fixed and added test to guarantee it never happens again.

fixing the docs:
using UnityEngine;
namespace Mirror.Examples
{
public class PrefabPool : MonoBehaviour
{
// singleton for easier access from other scripts
public static PrefabPool singleton;
[Header("Settings")]
public GameObject prefab;
[Header("Debug")]
public int currentCount;
public Pool<GameObject> pool;
void Start()
{
InitializePool();
singleton = this;
NetworkClient.RegisterPrefab(prefab, SpawnHandler, UnspawnHandler);
}
// used by NetworkClient.RegisterPrefab
GameObject SpawnHandler(SpawnMessage msg) => Get(msg.position, msg.rotation);
// used by NetworkClient.RegisterPrefab
void UnspawnHandler(GameObject spawned) => Return(spawned);
void OnDestroy()
{
NetworkClient.UnregisterPrefab(prefab);
}
void InitializePool()
{
// create pool with generator function
pool = new Pool<GameObject>(CreateNew, 5);
}
GameObject CreateNew()
{
// use this object as parent so that objects dont crowd hierarchy
GameObject next = Instantiate(prefab, transform);
next.name = $"{prefab.name}_pooled_{currentCount}";
next.SetActive(false);
currentCount++;
return next;
}
// Used to take Object from Pool.
// Should be used on server to get the next Object
// Used on client by NetworkClient to spawn objects
public GameObject Get(Vector3 position, Quaternion rotation)
{
GameObject next = pool.Get();
// set position/rotation and set active
next.transform.position = position;
next.transform.rotation = rotation;
next.SetActive(true);
return next;
}
// Used to put object back into pool so they can b
// Should be used on server after unspawning an object
// Used on client by NetworkClient to unspawn objects
public void Return(GameObject spawned)
{
// disable object
spawned.SetActive(false);
// add back to pool
pool.Return(spawned);
}
}
}
I am still getting the following error:
Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=339394605 netId=4
UnityEngine.Debug:LogError (object)
Mirror.NetworkClient:SpawnPrefab (Mirror.SpawnMessage) (at Assets/Mirror/Core/NetworkClient.cs:1137)
Mirror.NetworkClient:FindOrSpawnObject (Mirror.SpawnMessage,Mirror.NetworkIdentity&) (at Assets/Mirror/Core/NetworkClient.cs:1086)
Mirror.NetworkClient:OnSpawn (Mirror.SpawnMessage) (at Assets/Mirror/Core/NetworkClient.cs:1323)
Mirror.NetworkClient/<>c__DisplayClass53_01<Mirror.SpawnMessage>:<RegisterHandler>g__HandlerWrapped|0 (Mirror.NetworkConnection,Mirror.SpawnMessage) (at Assets/Mirror/Core/NetworkClient.cs:492)
Mirror.NetworkMessages/<>c__DisplayClass10_02<Mirror.SpawnMessage, Mirror.NetworkConnection>:2<Mirror.SpawnMessage, Mirror.NetworkConnection>:<WrapHandler>b__0 (Mirror.NetworkConnection,Mirror.NetworkReader,int) (at Assets/Mirror/Core/NetworkMessages.cs:152)
Mirror.NetworkClient:UnpackAndInvoke (Mirror.NetworkReader,int) (at Assets/Mirror/Core/NetworkClient.cs:304)
Mirror.NetworkClient:OnTransportData (System.ArraySegment11<byte>,kcp2k.KcpChannel) (at Assets/Mirror/Transports/KCP/MirrorTransport/KcpTransport.cs:107)
kcp2k.KcpClient:<Connect>b__8_1 (System.ArraySegment1