Description
This issue sometimes occurs as Client when connecting to a Host that shut down too quickly during the connection process. We can reproduce this quite reliably in our project when the connecting Client has a high latency (200+ ms) and there's at least two Clients connecting to the Host simultaneously. We haven't been able to reproduce this with only one connecting Client. We usually need to shut down and restart the Host multiple times before the error occurs, and we're not sure if it can happen on the first connection attempt.
The Client gets the following error:
NotServerException: Destroy a spawned NetworkObject on a non-host client is not valid. Call Destroy or Despawn on the server/host instead.
at Unity.Netcode.NetworkObject.OnDestroy () [0x000b0] in ***\Library\PackageCache\com.unity.netcode.gameobjects@1.2.0\Runtime\Core\NetworkObject.cs:451
With log level set to Developer in NetworkManager, the Client also gets the following warning about several NetworkObjects right after the exception:
[Netcode] NetworkBehaviour-SomeGameObject is being destroyed while NetworkObject-SomeGameObject is still spawned! (could break state synchronization)
UnityEngine.StackTraceUtility:ExtractStackTrace ()
UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
UnityEngine.Logger:Log (UnityEngine.LogType,object)
UnityEngine.Debug:LogWarning (object)
Unity.Netcode.NetworkLog:LogWarning (string) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Logging/NetworkLog.cs:28)
Unity.Netcode.NetworkObject:OnNetworkBehaviourDestroyed (Unity.Netcode.NetworkBehaviour) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkObject.cs:1476)
Unity.Netcode.NetworkBehaviour:OnDestroy () (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkBehaviour.cs:1029)
The full sequence of events is logged as follows (we can provide additional details if necessary):

Notes:
- The exception is thrown before
NetworkManager.OnClientConnectedCallback is handled on the Client.
- The Client also logs the following warning during the connection attempt, though I'm not sure if it's related to this issue:
Trying to send ServerLogMessage to client 0 which is not in a connected state. It seems to log it even during a connection attempt that doesn't throw the exception.
- We tried to work around this issue by destroying and recreating the NetworkManager before each connection attempt, but it didn't have an effect on the exception.
- We've checked that there are no NetworkObjects in the scene when the Client calls
NetworkManager.StartClient().
- Waiting for all Clients to finish loading the menu scene before starting a new server didn't have an effect on the issue.
Reproduce Steps
Difficult to reproduce. This is how we reproduce the error in our project:
- Join a Steam Lobby with Host and two Clients in a menu scene.
- Start a game as Host and load a level.
- As soon as the level loads, set the server available for Clients in the Steam Lobby.
- Automatically connect to Host as Clients as soon as the server becomes available and load the level.
- As Host, shut down the server almost immediately when it loads a level.
- As Clients, get disconnected from the Host and return to the menu scene.
- Repeat the above steps.
- As Client (with 200+ ms latency), notice that you sometimes get the
NotServerException and the game breaks.
Actual Outcome
Client gets a NotServerException when connecting to a Host (trying to destroy a spawned NetworkObject).
Expected Outcome
Client doesn't get a NotServerException when connecting to a Host (by trying to destroy a spawned NetworkObject).
Environment
- OS: Windows 10
- Unity version: 2021.3.19f1
- Netcode version: 1.2.0
Description
This issue sometimes occurs as Client when connecting to a Host that shut down too quickly during the connection process. We can reproduce this quite reliably in our project when the connecting Client has a high latency (200+ ms) and there's at least two Clients connecting to the Host simultaneously. We haven't been able to reproduce this with only one connecting Client. We usually need to shut down and restart the Host multiple times before the error occurs, and we're not sure if it can happen on the first connection attempt.
The Client gets the following error:
With log level set to
Developerin NetworkManager, the Client also gets the following warning about several NetworkObjects right after the exception:The full sequence of events is logged as follows (we can provide additional details if necessary):
Notes:
NetworkManager.OnClientConnectedCallbackis handled on the Client.Trying to send ServerLogMessage to client 0 which is not in a connected state.It seems to log it even during a connection attempt that doesn't throw the exception.NetworkManager.StartClient().Reproduce Steps
Difficult to reproduce. This is how we reproduce the error in our project:
NotServerExceptionand the game breaks.Actual Outcome
Client gets a
NotServerExceptionwhen connecting to a Host (trying to destroy a spawned NetworkObject).Expected Outcome
Client doesn't get a
NotServerExceptionwhen connecting to a Host (by trying to destroy a spawned NetworkObject).Environment