Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question NetworkBehaviour is being destroyed while NetworkObject is still spawned!

Discussion in 'Netcode for GameObjects' started by azeTrom17, Sep 8, 2023.

  1. azeTrom17

    azeTrom17

    Joined:
    Mar 17, 2022
    Posts:
    5
    I frequently receive the following warning:
    NetworkBehaviour is being destroyed while NetworkObject is still spawned! (could break state synchronization)

    This warning appears nearly anytime a NetworkObject is destroyed through means other than Destroy or Despawn.

    Here's a Reddit post I made showing how to easily recreate the warning by reloading the current scene:
    https://www.reddit.com/r/Unity3D/comments/16affyt/netcode_for_gameobjects_networkbehaviour_is_being/
    The warning also appears on a host whenever a client disconnects, if that client owns any gameobjects.

    Is there something wrong with allowing NetworkObjects to be destroyed automatically under these circumstances? I don't need to manually send an event to despawn all non-global NetworkObjects whenever a client disconnects or the scene unloads, do I?

    I'm hoping that there's something basic about Netcode for GameObjects that I'm not doing--I'm new to this Networking Solution. I wasn't able to find any answers in the documentation or in any tutorials.
     
  2. JethRow

    JethRow

    Joined:
    Sep 22, 2021
    Posts:
    10
    Whenever u want to destroy networkobject u have to despawn it first
     
    azeTrom17 likes this.
  3. azeTrom17

    azeTrom17

    Joined:
    Mar 17, 2022
    Posts:
    5
    So if I change scenes, I should first send an event that manually despawns all non-global networkbehaviours? Why isn't that built into scene unloading already?
    Much more importantly, how should I go about despawning network objects that are owned by a client before a client disconnects? Say a non-host client owns a Player network object, and their computer crashes. The player object will be destroyed on the host as well, and the warning will appear. How would I ensure that it gets despawned first? And again, why isn't this built in?
    Thanks!
     
  4. JethRow

    JethRow

    Joined:
    Sep 22, 2021
    Posts:
    10
    Well the client should only own their controls and nothing else.Since Host or server will usually be responsible for everything happening, and the host can also despawn any network object that he knows of and send that across network using clientRpc call, so it doesnt matter who owns it, just let host do it and tell everyone about it. So for your example when a client leaves a game and you want his objects to be despawned, just have parent of all of his object despawned, i assume its about playerprefab that holds everything else so just tell the host to despawn that playerprefab across the network. I hope this helped and sorry for late reply
     
  5. azeTrom17

    azeTrom17

    Joined:
    Mar 17, 2022
    Posts:
    5
    In some of my games, clients own multiple objects. Not all of them are built in a way that prevents cheating--many of my multiplayer games are casual.

    Anyway, if a client's computer crashes, for example, how would I despawn the object on the host before the error occurs? Can I rely on remote client disconnect events to occur on the host before the objects would attempt to forcefully despawn, causing the error? I doubt that but I could definitely be wrong.

    Again, I REALLY don't see why the correct process, whatever it is, isn't included in the documentation. This seems pretty central to Netcode, and I'm not seeing an obvious, intuitive solution. Given the amount of similar questions I've seen in various forums, and the lack of solutions in those forums, it seems to be a pretty pathetic oversight in the documentation.

    If you could be a bit more clear about how you're despawning the disconnected client's objects BEFORE they attempt to despawn automatically, that'd be helpful.

    Thanks again so much for your help, you're a lifesaver!