com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

How to prevent scene NetworkObjects from auto Spawning ?

Open Marc477 opened this issue 3 years ago • 2 comments

I have a scene with thousands of NetworkObjects, but only a few of them needs to be active at a time. If they are not spawned, everything would work smoothly, but scene objects are spawned automatically according to : https://docs-multiplayer.unity3d.com/netcode/current/basics/object-spawning/index.html

How do I prevent this from happening? Is there a variable on the NetworkObject to prevent auto spawning? Otherwise I don't see how it's possible to use Netcode with a large number of NetworkObjects.

I'd like to be able to call NetworkObject.Spawn() myself and manage which ones should be synced. Instead of spawning everything and lagging the game. Anyone knows how to do this?

EDIT I tried to use CheckObjectVisibility, but the network code still loops on those objects every network tick, because those objects are still in the SpawnedObjects array, affecting performance. When an object isn't Spawned this does not happen. So I really need a way to not Spawn scene objects automatically.

Marc477 avatar Aug 04 '22 02:08 Marc477

Disabled game objects won't spawn. So have them disabled, and then only enable the ones you want.

TheCaveOfWonders avatar Aug 15 '22 14:08 TheCaveOfWonders

So i tried disabling inside the Start() function and it doesn't work, it will spawn and sync right away.

Disabling in the Awake() function works, it prevents the Spawn, but then if I call Spawn() later (i tried from the component UI on the server by clicking on Spawn) it doesn't sync the object with the one already existing on the client scene.

So in other words, it's impossible to Spawn() an object that was already in the scene if it was disabled during awake.

Marc477 avatar Sep 01 '22 00:09 Marc477

@Marc477 I am converting this feature to a bug as you should be able to set the GameObject with the attached NetworkObject component as in-active. After looking at where the disconnect is, I realized that there is an issue with this.

NoelStephensUnity avatar Oct 05 '22 21:10 NoelStephensUnity

This issue will be resolved in the next update.

NoelStephensUnity avatar Oct 07 '22 20:10 NoelStephensUnity

Cool, will it also be possible to Spawn() an object that was previously Despawn() ? So Spawn() and Despawn() at will without destroying the object ? So it keeps running on the server, but not on the client. And only sync with client under certain conditions. I think before there were issues with Spawn() an object that was previously Despawn()

Marc477 avatar Oct 08 '22 02:10 Marc477

@Marc477 For in-scene placed NetworkObjects you are in control of what is disabled and what is not. So, you could despawn an in-scene placed NetworkObject (passing false to not destroy) and disable it on all clients but on the server you could leave the root GameObject enabled... or you could pick and choose which components you wish to enable/disable like you can normally do in single player. You would just need to make sure to re-enabled whatever is disabled (if anything) on the server side before respawning the same in-scene placed NetworkObject.

NoelStephensUnity avatar Oct 11 '22 16:10 NoelStephensUnity