- Home /
Multiplayer Network Object Pooling Despawning intermittent issue
I’ve found a small problem with the following piece of code for which I found a workaround but would love to understand why it happens.
I’m using the example Network pooling code to spawn objects, specifically bullets. When the bullets impact I’m calling the despawn method (which is implemented by the network pooling code).
Most of the time this works just fine on the client and host. But I found that at times on the client some of the bullets would be left lying around. They appeared to be completely dormant, causing no collision, having no physics but shown as an active object on client side copy of the object pool. After some investigation I found it only seems to occur when I’m spawning inside an object causing an instant collision (in this case because I put the nose of the ship right into an object). The problem only occurs on the client and it doesn’t matter who is spawning the objects (in reality of course all objects are spawned from the server anyhow) and its very intermittent but spamming bullets when inside an object will cause the issue maybe 1 in 5 times so easily repeatable.
So this is a pared down version of the offending code:
private void FixedUpdate()
{
if (IsServer && m_Started)
{
if ((m_DestroyAtSec < Time.fixedTime))
{
NetworkObject networkObject = gameObject.GetComponent<NetworkObject>();
networkObject.Despawn();
}
}
}
void OnTriggerEnter(Collider col)
{
if (IsServer && m_Started)
{
NetworkObject networkObject = gameObject.GetComponent<NetworkObject>();
networkObject.Despawn(); // sometimes doesn’t work correctly
}
}
To fix this I performed the despawn during an the update instead of ontriggerenter.
private void FixedUpdate()
{
if (IsServer && m_Started)
{
if ((m_DestroyAtSec < Time.fixedTime) || m_isDead)
{
NetworkObject networkObject = gameObject.GetComponent<NetworkObject>();
networkObject.Despawn();
}
}
}
void OnTriggerEnter(Collider col)
{
if (IsServer && m_Started && m_isDead == false)
{
m_isDead = true;
}
}
I would like to understand what might the cause of the issue, was I doing something I shouldn’t?.
Link to the Netcode Object pooling:
https://docs-multiplayer.unity3d.com/docs/advanced-topics/object-pooling
Your answer
Follow this Question
Related Questions
How to spawn character on desired position using UNET/Customized Network Manager?? 0 Answers
Architecture for a network game- problems with events 0 Answers
How to avoid spawn delay on client? 0 Answers
UNet - How do I make Network.Spawn not show the prefab to the user that called it? 1 Answer
Object Pool on Server not spawning on new Clients (Mirror) 0 Answers