Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Scene placed NetworkObjects are automatically destroyed when they shouldn't be

Discussion in 'Netcode for GameObjects' started by digifoxreg, Mar 28, 2024.

  1. digifoxreg

    digifoxreg

    Joined:
    Sep 25, 2018
    Posts:
    6
    Hey, I'm having issues with Scene placed NetworkObjects being destroyed when server refuses client connection approval. This is the part of the code in NetworkSpawnManager.cs which is responsible for this:
    Code (CSharp):
    1.  
    2. internal void DespawnAndDestroyNetworkObjects()
    3. {
    4.      var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
    5.      ...
    6.      // If it is an in-scene placed NetworkObject then just despawn and let it be destroyed when the scene
    7.      // is unloaded. Otherwise, despawn and destroy it.
    8.      var shouldDestroy = !(networkObjects[i].IsSceneObject != null && networkObjects[i].IsSceneObject.Value);
    9.    
    10.      if (shouldDestroy)
    11.      {
    12.           UnityEngine.Object.Destroy(networkObjects[i].gameObject);
    13.      }
    14.      ...
    15. }
    16.  
    The problem that I'm facing is that when server rejects clients connection approval, these network objects are not yet spawned, so even though they are placed in scene, the IsSceneObject bool is false, as explained in this code:
    Code (CSharp):
    1.  /// <summary>
    2. /// Gets if the object is a SceneObject, null if it's not yet spawned but is a scene object.
    3. /// </summary>
    4. public bool? IsSceneObject { get; internal set; }
    So what happens is that these objects are immediately destroyed when they shouldn't be, which seems really odd to me. I'm not sure what to do to avoid this behavior, but it seems like a bug to me.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    For the client or the server?

    I have a hunch that you are entering the game in the same scene where clients join the network session. In that case, the scene on the client side is an offline scene, but it will then destroy any NetworkObjects in that scene if the client does not get approved but also likely when the client does get approved but later leaves the session or disconnects.

    It would be wise to have the server/host single-loads a scene with NetworkSceneManager, so that clients joining will also be pulled into that scene, but remain in their supposedly menu scene if approval fails.
     
  3. digifoxreg

    digifoxreg

    Joined:
    Sep 25, 2018
    Posts:
    6
    For client only.
    This is not the case, because in this situation the objects are already spawned... That means IsSceneObject will be true and NetworkSpawnManager.cs will just despawn the objects instead of destroying them. This only happens if the scene placed objects are not spawned yet.
    My current setup is that I have MainMenu scene from where I have 2 gameMode options - SinglePlayer, Coop. If player chooses Coop then NetworkManager Starts and everything stays in that scene. So you are saying I should move this logic to another scene?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Ideally yes. I would not mix offline with online scenes but keep them separate.

    But not for singleplayer?
    That means you're effectively programming each feature twice, and any change made to a working feature in one mode may break this or other features in the other mode.

    Make sure the singleplayer plays as host in an isolated network session (ip 127.0.0.1 and port = 0 effectively shut off the session from the outside). Then you won't have (m)any issues when moving a singleplayer feature to online coop because it's already programmed with networking in mind.
     
    unnanego likes this.
  5. ScetticoBlu

    ScetticoBlu

    Joined:
    Nov 26, 2020
    Posts:
    27
    I have a similar problem...
    I recently added the accept/refuse logic via
    NetworkManager.Singleton.ConnectionApprovalCallback
    .
    Before adding it everythings worked fine. Now as soon as a connection is refused, every network object gets destroyed...
    Please help me!
     
  6. ScetticoBlu

    ScetticoBlu

    Joined:
    Nov 26, 2020
    Posts:
    27
    Ok I've made some test and is something related to
    NetworkManager.Singleton.DisconnectClient(id)
    , not in the accept/refuse logic.
    If the client itself disconnects, by closing or calling
    NetworkManager.Singleton.Shutdown()
    , no problem.
    But if is the server that calls DisconnectClient, every networkObject disappears..
    Is it a bug?