RegisterHandler() works in the editor, but not in a build

I’m trying to register handlers for custom network messages. It works flawlessly in the editor, but now when I’m doing the same on a build.

When I’m trying to register a serverside handler as this, the NetworkManager HUD’s host / server button doesn’t execute anything past the RegisterHandler().

public class NetMessage
{
    public const short CHAT_MSG = MsgType.Highest + 1;
    public const short PLAYER_INFO = MsgType.Highest + 2;
}

public class Shared_NetHandler
{
    public static void Client_Register()
    {
        NetworkClient client = NetworkManagerEx.singleton.client;
        client.RegisterHandler(NetMessage.CHAT_MSG, Chat_Shared.Singleton.Client_ReceiveChatMessage);
        client.RegisterHandler(NetMessage.PLAYER_INFO, Shared_Steamworks.Singleton.Client_RequestSteamID);
    }

    public static void Server_Register()
    {
        NetworkServer.RegisterHandler(NetMessage.CHAT_MSG, Chat_Shared.Singleton.Server_ReceiveChatMessage);
        NetworkServer.RegisterHandler(NetMessage.PLAYER_INFO, NetworkManagerEx.AddPlayerInfo);
    }
}
public override void OnStartServer()
    {
        Chat_Client.Print("Started a server.");
        Chat_Client.Print(string.Format("Registering serverside handlers, {0} registered.", NetworkServer.handlers.Count));
        Shared_NetHandler.Server_Register();
        Chat_Client.Print(string.Format("Done registering serverside handlers, {0} registered.", NetworkServer.handlers.Count));
    }

    public override void OnClientConnect(NetworkConnection conn)
    {
        Chat_Client.Print(string.Format("Registering clientside handlers! C:{0}, S:{1}.", client.handlers.Count, NetworkServer.handlers.Count));
        Shared_NetHandler.Client_Register();
        Chat_Client.Print(string.Format("Done registering clientside handlers! C:{0}, S:{1}.", client.handlers.Count, NetworkServer.handlers.Count));

        ClientScene.Ready(conn);
        Chat_Client.Print("Client is ready");
        ClientScene.AddPlayer(0);

        Shared_NetHandler.Server_Register();
        Chat_Client.Print(string.Format("Joined a server as client! C:{0}, S:{1}.", client.handlers.Count, NetworkServer.handlers.Count));
    }

When I tried logging it, it came up with this in the editor and build.

If you change the log level in the NetworkManager to “Debug” it will log message handler registration. Maybe that will give more info?

ArgumentException: Value does not fall within the expected range.
Here’s the full log - pressed Start Host button and then closed myself.

Shared_NetHandler.cs:51

NetworkServer.RegisterHandler(NetMessage.CHAT_MSG, Chat_Shared.Singleton.Server_ReceiveChatMessage);

NetworkManagerEx.cs:31

Shared_NetHandler.Server_Register();
Mono path[0] = 'C:/Unity 5/2D Cave Game/Build/2/test2_Data/Managed'
Mono path[1] = 'C:/Unity 5/2D Cave Game/Build/2/test2_Data/Mono'
Mono config path = 'C:/Unity 5/2D Cave Game/Build/2/test2_Data/Mono/etc'
PlayerConnection initialized from C:/Unity 5/2D Cave Game/Build/2/test2_Data (debug = 0)
PlayerConnection initialized network socket : 0.0.0.0 55320
Multi-casting "[IP] 192.168.1.133 [Port] 55320 [Flags] 3 [Guid] 937473506 [EditorId] 636018801 [Version] 1048832 [Id] WindowsPlayer(Flippy) [Debug] 1" to [225.0.0.222:54997]...
Waiting for connection from host on [192.168.1.133:55320]...
PlayerConnection accepted from [192.168.190.1] handle:0x1c0
Using monoOptions --debugger-agent=transport=dt_socket,embedding=1,defer=y,address=0.0.0.0:56506
PlayerConnection already initialized - listening to [192.168.1.133:55320]
Initialize engine version: 5.1.2p3 (65486b59c7a9)
GfxDevice: creating device client; threaded=1
Direct3D:
    Version:  Direct3D 11.0 [level 11.0]
    Renderer: NVIDIA GeForce GTX 760 (ID=0x1187)
    Vendor:   NVIDIA
    VRAM:     4027 MB
Begin MonoManager ReloadAssembly
Platform assembly: C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\UnityEngine.dll (this message is harmless)
Loading C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\UnityEngine.dll into Unity Child Domain
Platform assembly: C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\Assembly-CSharp-firstpass.dll (this message is harmless)
Loading C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\Assembly-CSharp-firstpass.dll into Unity Child Domain
Platform assembly: C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\Assembly-CSharp.dll (this message is harmless)
Loading C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\Assembly-CSharp.dll into Unity Child Domain
Platform assembly: C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\UnityEngine.UI.dll (this message is harmless)
Loading C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\UnityEngine.UI.dll into Unity Child Domain
Platform assembly: C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\UnityEngine.Networking.dll (this message is harmless)
Loading C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\UnityEngine.Networking.dll into Unity Child Domain
Platform assembly: C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\UnityEngine.Analytics.dll (this message is harmless)
Loading C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\UnityEngine.Analytics.dll into Unity Child Domain
Platform assembly: C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\System.Core.dll (this message is harmless)
- Completed reload, in  0.046 seconds
desktop: 1920x1200 59Hz; virtual: 3840x1200 at -1920,0
<RI> Initializing input.

<RI> Input initialized.

<RI> Initialized touch support.

Platform assembly: C:\Unity 5\2D Cave Game\Build\2\test2_Data\Managed\System.dll (this message is harmless)
0
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
NetworkManagerEx:Start() (at C:\Unity 5\2D Cave Game\Assets\Resources\Experimental\NetworkManagerEx.cs:24)
(Filename: C:/Unity 5/2D Cave Game/Assets/Resources/Experimental/NetworkManagerEx.cs Line: 24)

ArgumentException: Value does not fall within the expected range.
  at Shared_NetHandler.Server_Register () [0x00000] in C:\Unity 5\2D Cave Game\Assets\Resources\Experimental\Shared_NetHandler.cs:51

  at NetworkManagerEx.OnStartServer () [0x00028] in C:\Unity 5\2D Cave Game\Assets\Resources\Experimental\NetworkManagerEx.cs:31

  at UnityEngine.Networking.NetworkManager.StartServer (UnityEngine.Networking.Match.MatchInfo info, UnityEngine.Networking.ConnectionConfig config, Int32 maxConnections) [0x00000] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkManager.cs:218

  at UnityEngine.Networking.NetworkManager.StartServer (UnityEngine.Networking.Match.MatchInfo info) [0x00000] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkManager.cs:213

  at UnityEngine.Networking.NetworkManager.StartServer () [0x00000] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkManager.cs:208

  at UnityEngine.Networking.NetworkManager.StartHost () [0x00006] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkManager.cs:393

  at UnityEngine.Networking.NetworkManagerHUD.OnGUI () [0x00075] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkManagerHUD.cs:67
(Filename: C:/Unity 5/2D Cave Game/Assets/Resources/Experimental/Shared_NetHandler.cs Line: 51)

Waiting for finish

ok, there is an exception registering your message handler. Is the Chat_Shared.Singleton initialized at the time?

Oh, it’s not. Turns out that I called it too early before the GameObjects were actually created.