Search Unity

Question Network objects losing reference when scripts recompile

Discussion in 'Netcode for GameObjects' started by Zyrica, Jun 21, 2021.

  1. Zyrica

    Zyrica

    Joined:
    Apr 16, 2015
    Posts:
    11
    Everytime the scripts recomplie the NetworkManager seam to lose all network object references. This forces my workflow to restart the game client with every iteration. I've tried to restart the network session on the UnityEditor.Callbacks.DidReloadScripts event, but since all the references is already lost it can't clean up and crashes.

    Just an example of how it looks:
    upload_2021-6-22_0-5-33.png

    Is there a workaround or a fix in order to be able to develop mlapi games without restarting (toggle the playbutton twice) the client every time?
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Can you post the stacktraces of the exceptions?
     
  3. Zyrica

    Zyrica

    Joined:
    Apr 16, 2015
    Posts:
    11
    Example stacktrace:
    upload_2021-6-22_11-55-0.png

    How I trigger it:
    1. Play-button to start game, it connects and some Network Object spawns in.
    2. I make some changes in my code (unrelated file) and the scripts recompile.
    3. Play-button to stop the game. I get one of these warnings for every Network Object that has been spawned in.
    If I skip step 2, it works fine without warnings.
     

    Attached Files:

  4. Zyrica

    Zyrica

    Joined:
    Apr 16, 2015
    Posts:
    11
    And before that i get these 2 errors:

    upload_2021-6-22_12-0-40.png

    upload_2021-6-22_12-0-51.png
     
  5. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    It looks like it might be an issue with the visual scripting package. The exception looks like it is nullifying your NetworkObject and fails for some reasons which leaves the MLAPI NetworkManager in this broken state.
     
  6. Zyrica

    Zyrica

    Joined:
    Apr 16, 2015
    Posts:
    11
    I'm not using any visual scripting.
     
  7. Zyrica

    Zyrica

    Joined:
    Apr 16, 2015
    Posts:
    11
    I've found a workaround, that keeps me going for the moment.

    I disconnect on compilationStarted and reconnect after the DidReloadScripts event. Suboptimal since i lose the state, but ok for now.
     
  8. Then you should uninstall the package from your project.
     
  9. Zyrica

    Zyrica

    Joined:
    Apr 16, 2015
    Posts:
    11
    Ok, so I uninstalled it. Now I don't get that error. I still get the warnings:
    upload_2021-6-22_16-32-22.png

    And if I try to call an ServerRpc on one of the GameObjects, after the scripts have been compiled. Then I get the following error.
    upload_2021-6-22_16-34-50.png
     
  10. Zyrica

    Zyrica

    Joined:
    Apr 16, 2015
    Posts:
    11
    I found my fault and successfully fixed it :D I wasn't handling NetworkVariable listeeners correctly.

    I went from this:
    upload_2021-6-23_15-10-46.png

    To this:
    upload_2021-6-23_15-11-56.png

    And now the network object doesn't get lost during script compilation :D

    I know it was a silly mistake, it often is, but it was hard to figure out from the stack traces.
     

    Attached Files: