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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question NetworkManager's Player Prefab is being destroyed on Scene Loading on the Client

Discussion in 'Netcode for GameObjects' started by GuirieSanchez, Nov 20, 2022.

  1. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    upload_2022-11-20_18-12-10.png

    So, I'm experiencing this issue where the player prefabs get destroyed on the client side even when they got a "Don't Destroy on Load" script attached to them:


    Code (CSharp):
    1. public class DontDestroyOnLoad : MonoBehaviour
    2.     {
    3.         void Awake()
    4.         {
    5.             DontDestroyOnLoad(gameObject);
    6.         }
    7.     }
    The weird thing is that it was working fine so far, I've tested it like 100 times over the course of 2 weeks, and they never got destroyed till now.

    upload_2022-11-20_18-19-41.png

    I've tried toggling on and off this parameter on the "NetworkObject" to see if that helps, but the outcome is the same.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    1,620
    Check the callstack of the first error message and see where in your own code (if any) this call originates from. Check any script that may attempt to destroy the player prefab instance.

    Do you spawn player prefabs manually?
    Do you use NetworkSceneManager to change scenes?
    Do you perhaps call StartClient() and then perform a scene change on the client side? When a client connects, it will automatically load the scene the server is running.
     
  3. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    upload_2022-11-20_18-29-10.png

    I got this Stack Trace activated, but I got no more info than the one that shows here:

     
  4. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    No, it's done automatically.
    Yes, I do it from the Server/Host

    So, in theory the "NetworkSceneManager.LoadScene" is called from a function with this [ServerRpc(RequireOwnership = false)], and solely gets triggered only when all clients are ready (so it's called only once). Also, before using the "NetworkSceneManager.LoadScene" there is an if statement:
    Code (CSharp):
    1. if (IsServer && NetworkManager.IsListening)
    2.             {
    3.                 if (_gameSceneName == null)
    4.                 {
    5.                     Net.LogError("NetworkSceneLoader has no on-demand scene assigned");
    6.                     return;
    7.                 }
    8.              
    9.                 Net.LogInfo($"Try load scene: {_gameSceneName}");
    10.                 var status = NetworkManager.SceneManager?.LoadScene(_gameSceneName, LoadSceneMode.Single);
    11.                 if (status != SceneEventProgressStatus.Started)
    12.                     Net.LogWarning($"Failed to load Scene '{_gameSceneName}' => {status}");
    13.             }
    So, only the server would be able to use the scene loading
     
    Last edited: Nov 20, 2022
  5. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    Does anyone have any idea?
     
  6. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    105
    Hi @GuirieSanchez ,

    1. That option will just make the player's prefab instance stay if the player disconnects from the game, it should not have any impact on your situaiton.

    2. What NGO version are you using?
    3. Are any of your scripts destroying the player's object?
     
  7. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    Hi, thanks for replying.

    The version I'm using is the 1.0.2

    None of my scripts do that. But just to make sure, I tried and find the word "destroy" on the entire solution, and indeed, none of my scripts are calling a destroy. It started to happen yesterday for the first time, but I tested the project a bunch of times prior and everything went as expected. It happens on the client (on the editor) after loading the scene on the host (and not always: from yesterday I believe it happens around 95% of the time).

    EDIT: It appears to happen only on the editor. If I host on the editor and join as a client on a build, the error doesn't show up (thus the player prefabs are not getting destroyed). This only happens if there are a host and a client, and the client is being played on the editor.
    But it's even weirder the fact that if I use 2 clients and a host, say, build 1 hosting, build 2 as client 1, and the editor as client 2, then the error doesn't show up.
     
    Last edited: Nov 21, 2022
  8. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    105
    Could you try updating to 1.1.0, to see if it reproduces?

    Was there something you added, yesterday? Do you a git history you can check?
     
  9. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    upload_2022-11-21_15-9-1.png

    Do I have to update it from the website? If I remove it, will it produce any incompatibility with my existing netcode's objects?

    I checked the history of the source control but I haven't seen anything suspicious.
     
  10. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    105
    @GuirieSanchez Netcode 1.1.0 is available on 2020 LTS and higher, it might not be available in your Unity version tho...

    As you can see in the changelog it has plenty of fixes and some changes. If you feel like you wouldn't be imapcted, maybe you can try upgrading?


    upload_2022-11-21_17-57-13.png
     
    Last edited: Nov 22, 2022
  11. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    This is weird. My Unity version is this one:

    upload_2022-11-21_21-32-49.png
     
  12. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    105
    Oh, it looks like I have 1.1.0 because I'm using

    Code (CSharp):
    1. "com.unity.multiplayer.samples.coop": "https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop.git?path=/Packages/com.unity.multiplayer.samples.coop#main",
    in my manifest, which has 1.1.0 as a dependency...
     
  13. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    105
    Hey @GuirieSanchez , did you manage to solve the issue? If yes, what was the cause?
     
  14. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    Hi, today I tried a couple of times and it seems to be working: the prefabs didn't get destroyed. However, it also worked yesterday until it didn't. Unfortunately, I'm afraid I can't be of help in order to track the bug since I don't have any clue of what the cause might be...

    If the problem disappears completely or I'm able to track the bug I'll let you know for sure.

    PS: I couldn't test it on 1.1.0 because I have no access to this version, so the update on the situation is on the 1.0.2 version, which might not be helpful.

    Anyway, thanks for helping!
     
    RikuTheFuffs-U likes this.