Skip to content

DontDestroyWithOwner does not work #3472

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

Closed
TjDMC opened this issue Jun 2, 2025 · 2 comments
Closed

DontDestroyWithOwner does not work #3472

TjDMC opened this issue Jun 2, 2025 · 2 comments
Labels
priority:high This issue has high priority and we are focusing to resolve it type:bug Bug Report

Comments

@TjDMC
Copy link

TjDMC commented Jun 2, 2025

Description

DontDestroyWithOwner = true does not work with SpawnWithObservers = false

Reproduce Steps

Create a new scene.
Setup NetworkManager.
Attach the GameManager script to some object.
Create some sample NetworkObject.
Set the references of GameManager.
Build project.
In editor, press S to start server and spawn sample NetworkObject.
In built app, press C to start client.
In editor, press T to trigger change ownership.
In built app, press C to disconnect.
In editor, spawned NetworkObject is destroyed.

using Unity.Netcode;
using UnityEngine;

public class GameManager : MonoBehaviour {
    [SerializeField] private NetworkManager networkManager;
    [SerializeField] private NetworkObject testObject;

    private NetworkObject testInstance;

    private void Update() {
        if (Input.GetKeyDown(KeyCode.S)) {
            networkManager.StartHost();

            testInstance = Instantiate(testObject);
            testInstance.SpawnWithObservers = false;
            testInstance.DontDestroyWithOwner = true;
            testInstance.Spawn();
        }

        if (Input.GetKeyDown(KeyCode.C)) {
            if (networkManager.IsServer || networkManager.IsClient) {
                networkManager.Shutdown();
            } else {
                networkManager.StartClient();
            }
        }

        if (Input.GetKeyDown(KeyCode.T)) {
            testInstance.NetworkShow(1);
            testInstance.ChangeOwnership(1); //also throws an error client-side
        }
    }
}

Actual Outcome

Network object is destroyed

Expected Outcome

Network object not destroyed

Environment

  • Unity Version: 6000.0.41f1
  • Netcode Version: 2.3.2
@TjDMC TjDMC added type:bug Bug Report stat:awaiting-triage Status - Awaiting triage from the Netcode team. stat:reply-needed Awaiting reply from Unity account labels Jun 2, 2025
@NoelStephensUnity NoelStephensUnity added priority:high This issue has high priority and we are focusing to resolve it stat:import Status - Issue is going to be saved internally and removed stat:awaiting-triage Status - Awaiting triage from the Netcode team. labels Jun 2, 2025
@NoelStephensUnity
Copy link
Collaborator

NoelStephensUnity commented Jun 2, 2025

@TjDMC,
I have replicated the core issue with the Dont Destroy With Owner property.

The issue with the back-to-back invocations of NetworkShow and ChangeOwnership followed by an error on the target client side and the Dont Destroy With Owner property not being honored upon a client disconnecting will both be resolved in v2.4.0.

@github-actions github-actions bot added stat:awaiting-response Awaiting response from author. This label should be added manually. and removed stat:reply-needed Awaiting reply from Unity account labels Jun 2, 2025
@NoelStephensUnity
Copy link
Collaborator

Closing as #3477 resolves this issue.

@github-actions github-actions bot removed stat:awaiting-response Awaiting response from author. This label should be added manually. stat:import Status - Issue is going to be saved internally labels Jun 3, 2025
EmandM pushed a commit that referenced this issue Jun 3, 2025
)

Based on issue #3472, this PR resolves the issue where the
`NetworkObject.DontDestroyWithOwner` was not being honored.

<!-- Add short version of the JIRA ticket to the PR title (e.g. "feat:
new shiny feature [MTT-123]") -->

## Changelog

- Fixed: Issue where the `NetworkObject.DontDestroyWithOwner` was not
being honored.

## Testing and Documentation

- Includes integration test
`NetworkObjectDontDestroyWithOwnerTests.NetworkShowThenClientDisconnects`.
- No documentation changes or additions were necessary.


<!-- Uncomment and mark items off with a * if this PR deprecates any
API:
### Deprecated API
- [ ] An `[Obsolete]` attribute was added along with a `(RemovedAfter
yyyy-mm-dd)` entry.
- [ ] An [api updater] was added.
- [ ] Deprecation of the API is explained in the CHANGELOG.
- [ ] The users can understand why this API was removed and what they
should use instead.
-->

## Backport
The v1.x branch does no have this issue, but went ahead and backported
the test in #3478.

<!-- If this is a backport:
 - Add the following to the PR title: "\[Backport\] ..." .
 - Link to the original PR.
If this needs a backport - state this here
If a backport is not needed please provide the reason why.
If the "Backports" section is not present it will lead to a CI test
failure.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority:high This issue has high priority and we are focusing to resolve it type:bug Bug Report
Projects
None yet
Development

No branches or pull requests

2 participants