Get access to player object on disconnect before despawn

I followed the advice in this post about reparenting network objects (or not in this case): Documentation on parenting NetworkObjects seems contradictory - #2 by CodeSmile

When a player picks up an entity, the entity is unparented from its network object and reparented to the player. When the player puts down the object, it is reparented back to the network object.

All works great except when a player disconnects, the object is despawned along with the player. I would like to be able to reparent the object before this happens but the only event I could find is NetworkManager.Single.OnClientDisconnectCallback and by the time it runs, the player object has already been despawned.

I considered whether I might be able to just spawn a new object but I can’t even get access to the player’s last position before disconnect so I wouldn’t know where to place it.

Any ideas?

On the object’s NetworkObject, check the checkbox “Don’t Destroy With Owner” and it should stick around.

1 Like

At least one of the entity’s script is a NetworkBehaviour, right? If not, the player could forward this. You will receive a OnNetworkDespawn for the object. You just need to find a way to determine if this was an expected despawn, or one that isn’t expected and thus must be due to a disconnect.

At that point you may still be able to reparent the object, but if not, you could definitely instantiate a copy.

1 Like