NetworkManager detected a script reload in the editor.

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:

public partial class ClientNetworkManager : Manager {
//(Manager : NetworkManager) is a shared class between the server and client managers, used to initialize variables like connections/prefabs/ip:port/...
    string username,password;

    static ClientNetworkManager(){
        //Used to parse server IP from config file.
    }

    void Awake(){
        base.Initialize ();
    }

    public void Connect(){
        username = GameObject.Find("UsernameInput").GetComponent<InputField>().text;
        password = GameObject.Find ("PasswordInput").GetComponent<InputField> ().text;
        if (client == null)
            client = new NetworkClient ();
        if (client.isConnected)
            client.Disconnect ();
        client.Configure (base.networkConfig, base.maxConnections);
        client.Connect (networkAddress, networkPort);
    }
}

Any idea what is causing the spawn of 2nd network manager?? and how to fix it??

I’m having the same issue. Does anyone know what this means?

Same issue here. Got rid of the second GameObject popping up using this code, but the script reload warning persisted. Anyone got ideas here?

private static UnetEngine _singelton;
void Awake()
    {
        if(_singelton == null)
        {
            _singelton = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            DestroyImmediate(gameObject);
        }
    }

Your solution is destroying the game object, but that doesn’t solve anything since it was already created and the script was reloaded :confused:
Would be awesome if anyone can provide a fix for this, or if needed downgrade to which version to fix it?

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.

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.

@VinQbator : Are there any error events you could listen to in your case?

I am using MsgType.Error handler, but there are no errors,

Remove Awake() function in your ClientNetworkManager and Manager.Because NetworkManager has already had Awake(). Put Initialize() to OnStartClient() something like that.

16 Likes

If it helps anyone, we realized that we were missing a } to close NetworkBehaviour - oops.

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.

2 Likes

You are amazing, but unet could do a better job. That wasn’t obvious

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.

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.

1 Like

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.