Search Unity

NetworkManager detected a script reload in the editor.

Discussion in 'Editor & General Support' started by Severos, Jan 29, 2016.

  1. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    I have an empty object that has network manager script attached to it, when I run the game in editor I get the error:
    NetworkManager detected a script reload in the editor. This has caused the network to be shut down.
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    after a closer look I found that there's a 2nd network manager object spawning in the scene.
    I was running 5.2f1 and had that problem, updated to 5.3f1 and still have it, I tried the NetworkTransport.Shutdown() then NetworkTransport.Init() and it didn't work. However when I tried to build and run the build executable it worked without problems.

    The scene have 2 input boxes and a button (username, password, login), here's the code used:

    Code (CSharp):
    1. public partial class ClientNetworkManager : Manager {
    2. //(Manager : NetworkManager) is a shared class between the server and client managers, used to initialize variables like connections/prefabs/ip:port/...
    3.     string username,password;
    4.  
    5.     static ClientNetworkManager(){
    6.         //Used to parse server IP from config file.
    7.     }
    8.  
    9.     void Awake(){
    10.         base.Initialize ();
    11.     }
    12.  
    13.     public void Connect(){
    14.         username = GameObject.Find("UsernameInput").GetComponent<InputField>().text;
    15.         password = GameObject.Find ("PasswordInput").GetComponent<InputField> ().text;
    16.         if (client == null)
    17.             client = new NetworkClient ();
    18.         if (client.isConnected)
    19.             client.Disconnect ();
    20.         client.Configure (base.networkConfig, base.maxConnections);
    21.         client.Connect (networkAddress, networkPort);
    22.     }
    23. }
    Any idea what is causing the spawn of 2nd network manager?? and how to fix it??
     
  2. AllMightyNico

    AllMightyNico

    Joined:
    Jun 12, 2015
    Posts:
    14
    I'm having the same issue. Does anyone know what this means?
     
  3. VinQbator

    VinQbator

    Joined:
    Aug 9, 2014
    Posts:
    12
    Same issue here. Got rid of the second GameObject popping up using this code, but the script reload warning persisted. Anyone got ideas here?
    Code (CSharp):
    1. private static UnetEngine _singelton;
    2. void Awake()
    3.     {
    4.         if(_singelton == null)
    5.         {
    6.             _singelton = this;
    7.             DontDestroyOnLoad(gameObject);
    8.         }
    9.         else
    10.         {
    11.             DestroyImmediate(gameObject);
    12.         }
    13.     }
     
  4. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Your solution is destroying the game object, but that doesn't solve anything since it was already created and the script was reloaded :/
    Would be awesome if anyone can provide a fix for this, or if needed downgrade to which version to fix it?
     
  5. janne_rasanen

    janne_rasanen

    Joined:
    Feb 5, 2016
    Posts:
    3
    I worked around this bug today. When the bug happens, NetworkManager.OnStopClient() gets called.

    In my code, I'm using a class that is derived from NetworkManager. I override the OnStopClient method. If the game is hosting, before calling CreateMatch I set up a flag. If OnStopClient is called and this flag is active, I wait a small while and then call CreateMatch again.

    Waiting is there because calling it instantly from the callback messes something up. Most likely the NetworkManager needs a frame to reset something or some other code is called after the callback.

    And I handle connecting to existing game the same way.

    Let me know if you need actual code.
     
  6. VinQbator

    VinQbator

    Joined:
    Aug 9, 2014
    Posts:
    12
    In my case, I'm not even using NetworkManager(or something derived from it), just NetworkServer and NetworkClient. The script reload hapens when calling Client.Connect(), code doesn't even reach MsgType.Connect handler.
     
  7. janne_rasanen

    janne_rasanen

    Joined:
    Feb 5, 2016
    Posts:
    3
    @VinQbator : Are there any error events you could listen to in your case?
     
  8. VinQbator

    VinQbator

    Joined:
    Aug 9, 2014
    Posts:
    12
    I am using MsgType.Error handler, but there are no errors,
     
  9. fish1725

    fish1725

    Joined:
    Jun 2, 2015
    Posts:
    2
    Remove Awake() function in your ClientNetworkManager and Manager.Because NetworkManager has already had Awake(). Put Initialize() to OnStartClient() something like that.
     
  10. ChrisAcoma

    ChrisAcoma

    Joined:
    Mar 25, 2016
    Posts:
    1
    If it helps anyone, we realized that we were missing a } to close NetworkBehaviour - oops.
     
  11. Glicknork

    Glicknork

    Joined:
    Jun 19, 2016
    Posts:
    4
    This fixed it for me - thanks! Guess you can't use Awake in a derived NetworkManager. I put it in OnEnable instead (next in the execution order) and it worked great.
     
    ceitel and dearamy like this.
  12. FarhanGul

    FarhanGul

    Joined:
    Jul 19, 2017
    Posts:
    2
    You are amazing, but unet could do a better job. That wasn't obvious
     
  13. saindon624

    saindon624

    Joined:
    Apr 5, 2018
    Posts:
    1
    OH GOD THANKS!!! I've been trying to understand a whole day why my client won't listen to broadcast and my NetworkDiscovery was throwing NullReferenceException. Removed Awake() in my custom NetworkManager and that fixed it.
     
  14. golergka

    golergka

    Joined:
    Mar 15, 2012
    Posts:
    30
    Since this is the first Google result for this error, I think to do a write-up of our solution for this problem in this thread.

    The things is, we don't use high-level Unity API at all. We used to do it, before, but now we re-implemented our own networking on top of Unity's low-level API. But still, when we transitioned from the "main menu" scene to the "game" scene, I got this error every time, and low-level transport API had been reset.

    After digging through Unity's Networking sources I realized that this check happens in static code, but to trigger it, there have to be two conditions met. First, a static boolean variable flips to true every time ther's a script reset happening (which happens every time you click "play", which makes sense). But for this to check to actually complete and this variable to turn false, tehre has to be a "pending singleton", which assigns itself in the constructor (although we're usually told not to use MonoBehavior's constructors, in this case it seems to be accepted practice, at least inside Unity).

    This was surprising, as we didn't have any NetworkManagers in the scene anymore. However, what we have in the game scene is the "prefab storage", a central source to all the prefabs we use in the game (so we can load them in appropriate places via string ids and without using godawful resource folders). And it turns out, that throughout these link graph, somewhere, somehow, we still had a reference to our old NetworkManager prefab.

    Turns out (now I'm hypothesizing a little bit) that Unity goes through all the linked prefabs and checked them (for example, printing warnings about old prefabs with components attached that since got turned into abstract classes or not-Monobehaviours). And assumingly, while going through this prefab tree and checking them, Unity's actually calling their constructors methods as well - and when it called the old NetworkManager's constructor, it assigned itself to the "pending singleton" field, triggering the check. (I verifyed this by writing a debug script that displayed NetworkManager's private static fields through reflection.)

    The fix turned out to be extremely simple: after I deleted old NetworkManager's prefab from the project, it stopped happening. Still, it's a very surprising and not obvious behaviour from Unity - I would appreciate if this under-the-hood stuff would be better documented. Or may be it is and I just wasn't reading the manual properly? In this case, a link to explanation of all this checks would be appreciated.
     
    austinborden likes this.
  15. austinborden

    austinborden

    Joined:
    Aug 5, 2016
    Posts:
    24
    Thank you for this tip. I'm not sure how my custom NetworkManager prefab is causing this script reloading issue either. The prefab object is never used through code, it's just in a couple scenes that share some networking settings. The script reload happens in a totally separate scene that doesn't use the prefab. Moving the prefab out of my Resources folder didn't do anything, but breaking the prefab connections and deleting the prefab altogether seems to have fixed the issue for me.