Skip to content
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

Detach but no longer active objects fail to set parent #818

Open
shadowfinderstudios opened this issue Nov 18, 2024 · 6 comments
Open

Detach but no longer active objects fail to set parent #818

shadowfinderstudios opened this issue Nov 18, 2024 · 6 comments
Labels
Bug - Harmless Result is unexpected but is not causing any issues. Resolved Pending Release

Comments

@shadowfinderstudios
Copy link

shadowfinderstudios commented Nov 18, 2024

General
Unity version: 6000.0.26f1
Fish-Networking version: 4.5.5R
Discord link: https://discord.com/channels/424284635074134018/851790045534748702/1308093185763049524

Description
When the game shuts down there is a possibility of an object that is no longer active but detached from the parent, as a result the SetParent is attempted on it but it isn't active in the hierarchy and causes an exception during shutdown. In addition, NetworkBehavior can attempt to OnDestroy an already null object during this time as well.

Replication
Steps to reproduce the behavior:

  1. Create a lot of objects and play your game.
  2. Suddenly shut down your game.
  3. See the exceptions.

Expected behavior
No exceptions on shutdown.

Code to fix the issue
In ResetState() in ChildTransformTickSmoother.cs:

if (_detach && _graphicalObject.gameObject.activeInHierarchy) { _graphicalObject.SetParent(_rootTransform); }

And, in NetworkObject.cs change network destroy to:

void NetworkBehaviour_OnDestroy() { foreach (NetworkBehaviour nb in NetworkBehaviours) { if (nb != null) nb.NetworkBehaviour_OnDestroy(); } }

@FirstGearGames
Copy link
Owner

Can you elaborate on what parts you're adding these, or what lines you are modifying?

@shadowfinderstudios
Copy link
Author

shadowfinderstudios commented Nov 22, 2024

Not sure if line numbers will help as much since those can change.

In ResetState() in ChildTransformTickSmoother.cs at line 654, change :
if (_detach) { _graphicalObject.SetParent(_rootTransform); }

To:
if (_detach && _graphicalObject.gameObject.activeInHierarchy) { _graphicalObject.SetParent(_rootTransform); }

And, NetworkObject.cs line 533 in NetworkBehaviour_OnDestroy, change:

void NetworkBehaviour_OnDestroy() { foreach (NetworkBehaviour nb in NetworkBehaviours) { nb.NetworkBehaviour_OnDestroy(); } }

To:
void NetworkBehaviour_OnDestroy() { foreach (NetworkBehaviour nb in NetworkBehaviours) { if (nb != null) nb.NetworkBehaviour_OnDestroy(); } }

Also, I've recently encountered another issue in NetworkObject at line 518-ish where a NetworkObject in the RunTimeParentNetworkBehaviour can be null by the time the program is shut down. The following fix of adding an extra != null can fix it, as below:

        Observers.Clear();
        if (NetworkBehaviours.Count > 0)
        {
            NetworkBehaviour thisNb = NetworkBehaviours[0];
            if (RuntimeParentNetworkBehaviour != null)
            {
                if (RuntimeParentNetworkBehaviour.NetworkObject.RuntimeChildNetworkBehaviours != null)
                    RuntimeParentNetworkBehaviour.NetworkObject.RuntimeChildNetworkBehaviours.Remove(thisNb);
            }
        }

@FirstGearGames FirstGearGames added Bug - Harmless Result is unexpected but is not causing any issues. and removed Bug Something isn't working labels Dec 2, 2024
@FirstGearGames
Copy link
Owner

Appreciate it.

@edunad
Copy link

edunad commented Dec 11, 2024

Having the null issue mentioned above, the null check fixes it
{41329265-4F05-4747-9604-3087A4628ED4}

@FirstGearGames
Copy link
Owner

FirstGearGames commented Dec 11, 2024 via email

@FirstGearGames
Copy link
Owner

I'm changing the detach check to this, which should only try to reattach if not shutting down the game or stopping play mode

                    if (_detach && !ApplicationState.IsQuitting())
                        _graphicalObject.SetParent(_rootTransform);

The runtimeparent one should already be fixed.
Please let me know if issue persist for the graphical one in 4.5.7 release.
If you're still getting the runtimeparent one as well, make a new issue.

FirstGearGames pushed a commit that referenced this issue Jan 14, 2025
- Fixed TimeManager.TimePassed negative time comparison (#829).
- Added Transport.GetPacketLoss. Only some transports support this feature.
- Added Tugboat.GetPacketLoss(bool).
- Added Server/ClientAttribute.UseIsStarted to force using IsServer/ClientStarted instead of Initialized.
- Fixed conditional NullReferenceException on clientHost when NetworkObserver deinitialized OnDestroy (#812).
- Renamed SceneManager.MoveClientHostObjects to MoveClientObjects.
- Changed SceneManager.MoveClientObjects now also applies to ClientOnly.
- Fixed NullReferenceExceptions on clientHost when rapidly spawning while changing scenes.
- Improved removed unnecessary logging for clientHost.
- Fixed NetworkObject NullReferenceException during OnDestroy when nested.
- Improved sending reconciles on Channel.Reliable now forces them through regardless if replicates run recently.
- Fixed Unity bug IL2CPP build crash for RPCs in generic classes. This is a limited work-around; for proper resolution use Unity 2022 or higher.
- Fixed OnServerDespawn not invoking via observer builds (thanks gooby!).
- Fixed math on Quaterion.Subtract extension.
- Fixed RigidbodyPauser unpausing to kinematic states when another Pauser made kinematic. This bug mostly affected OfflineRigidbody.
- Fixed invalid read size error when reader deserializers a null list (#837).
- Fixed ChildTransformTickSmoother graphics not moving with target when starting as clientHost then disconnecting client.
- Fixed ChildTransformTickSmoother moving to incorrect values when graphicalObject had an offset and when using adaptive interpolation.
- Fixed replicates not running default data when data was queued but interpolation was not met yet.
- Fixed preferred scenes not being set as active scene when SceneManager.SetActiveScene is false (#838).
- Fixed NetworkManager reference being null on reconcile and replicate readers, resulting in possible errors when reading types dependent on a NetworkManager.
- Fixed harmless NullReferenceException caused by ChildTransformTickSmoother when exiting play/application (#818).
- Fixed harmless Multipass Id not found error in Multipass when subsequentially starting server, client, then stopping server (#828).
- Improved performance slightly while in development mode when clients disconnect.
- Updated Edgegap to 3.0.9.
- Added CharacterController prediction demo, featuring: sprint, jumping, stamina, moving platforms, parenting.
- Added Rigidbody prediction demo, featuring: NetworkTrigger pickup, speed boost, predicting multiple rigidbodies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug - Harmless Result is unexpected but is not causing any issues. Resolved Pending Release
Projects
None yet
Development

No branches or pull requests

3 participants