Skip to content

fix: in-scene placed NetworkObject will not spawn client-side when disabled upon being despawned [MTT-4832] #2239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

NoelStephensUnity
Copy link
Collaborator

@NoelStephensUnity NoelStephensUnity commented Oct 5, 2022

If an in-scene placed NetworkObject's GameObject was disabled when it was despawned, then when the server re-spawned the same in-scene placed NetworkObject the client-side instances would never invoke NetworkBehaviour.OnNetworkSpawn.

MTT-4832
Pertains to #2104

Changelog

  • Fixed: issue where an in-scene placed NetworkObject would not invoke NetworkBehaviour.OnNetworkSpawn if the GameObject was disabled when it was despawned.

Testing and Documentation

  • Includes integration tests.
  • Requires updates to documentation (PR-816).

NoelStephensUnity and others added 8 commits October 5, 2022 16:51
This resolves the issue with in-scene placed NetworkObjects that are disabled when despawned not being able to re-spawn again.
Minor adjustments to manually validate the fixes.
Integration test will follow.
This update handles synchronizing despawned in-scene placed NetworkObjects during a scene switch (LoadSceneMode.Single).
Also fixes an exception when no in-scene placed NetworkObject is found.
Cache the NetworkManager to avoid exception during OnDestroy.
Added integration test that validates disabling NetworkObjects when despawned works with currently connected clients, late joining clients, and when scene switching (LoadSceneMode.Single) while also having the server despawn the in-scene placed NetworkObject upon its first spawn (i.e. so it starts off not visible/active to the clients when they finish the scene switch).
@NoelStephensUnity NoelStephensUnity marked this pull request as ready for review October 6, 2022 17:56
@NoelStephensUnity NoelStephensUnity requested a review from a team as a code owner October 6, 2022 17:56
This fixes the issue with the late joining client not being destroyed and removed as well as the issue with not resetting the new properties added to NetworkObjectTestComponent.
adding includeInactive: to FindObjectsOfType where this was changed to true in order to make it easier for reviewers to spot the change.
@NoelStephensUnity NoelStephensUnity merged commit e10c266 into develop Oct 7, 2022
@NoelStephensUnity NoelStephensUnity deleted the fix/in-scene-placed-networkobject-wont-spawn-clientside-when-disabled-upon-despawn-MTT-4832 branch October 7, 2022 20:54
@Marc477
Copy link

Marc477 commented Oct 8, 2022

With this fix, 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() but not destroyed

@NoelStephensUnity
Copy link
Collaborator Author

@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.

jakobbbb pushed a commit to GooseGirlGames/com.unity.netcode.gameobjects that referenced this pull request Feb 22, 2023
…sabled upon being despawned [MTT-4832] (Unity-Technologies#2239)

* fix
This resolves the issue with in-scene placed NetworkObjects that are disabled when despawned not being able to re-spawn again and handles synchronizing despawned in-scene placed NetworkObjects during a scene switch (LoadSceneMode.Single).

* test
Added integration test that validates disabling NetworkObjects when despawned works with currently connected clients, late joining clients, and when scene switching (LoadSceneMode.Single) while also having the server despawn the in-scene placed NetworkObject upon its first spawn (i.e. so it starts off not visible/active to the clients when they finish the scene switch).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants