Search Unity

Question Multiplay API is not initialized

Discussion in 'Game Server Hosting' started by AlduousMcBurlington, Jan 18, 2024.

  1. AlduousMcBurlington


    May 23, 2017
    To start, I'm building a multiplayer game hosted with Unity's Game Server Hosting, and I keep encountering the same error. I'm happy to provide more details about the code and setup.

    Long story short: Multiplay.Instance is never being set, causing an InvalidOperationException to be thrown when I try to access it. This error is not thrown in the editor, and only in a build for the server. Everything else seems to work, but I'm unable to call MultiplayService.Instance.ReadyServerForPlayersAsync() because of it, so it's entirely blocking connection until that can be invoked (as you'd assume).

    The flow of the program has been thoroughly tested, and there are no exceptions thrown before that; I'm calling UnityServices.InitializeAsync(), checking I get a true value returned to validate success, and only attempting to call MultiplayService.Instance.ReadyServerForPlayersAsync() after that has been checked and UnityServices.State == ServicesInitializationState.Initialized evaluates to true. This leads me to believe that Multiplay is not being registered as a service (since the property in question is just grabbing MultiplayServiceSdk.Instance and throwing the aforementioned exception if that reference is null), so I can only assume it's never initialized when UnityServices.InitializeAsync() is called.

    Yes, the project compiles without error; yes, the Build is uploaded directly; yes, the Build Configuration is set up properly; and, yes, all code is running flawlessly before that single call. The project is linked to UGS, I have debug logs to verify the success of everything I've explained above, and I'm thoroughly convinced I've followed every example and tutorial available on the entire internet.

    Below is the pertinent error received when trying to call UnityServices.InitializeAsync(), including my leading debug logs; please, for the sake of my sanity, somebody save me.

    StartServer() call succeeded.
    Waiting to start server
    UnityServices initialized with InitializeAsync().
    Server started.
    UnityServices.State: Initialized
    Readying up and waiting for players
    InvalidOperationException: Unable to get IMultiplayService because Multiplay API is not initialized. Make sure you call UnityServices.InitializeAsync();
    at Unity.Services.Multiplay.MultiplayService.InitializeWrappedMultiplayService () [0x0001d] in <7098dc2e4f424a4e9f85f11159e6e437>:0
    at Unity.Services.Multiplay.MultiplayService.get_Instance () [0x00007] in <7098dc2e4f424a4e9f85f11159e6e437>:0
    at GameServer.ReadyServerAndWait () [0x0000a] in <1f3a14fdb43e46a08e2e31cb437d6382>:0
    at GameServer.Initialize () [0x00038] in <1f3a14fdb43e46a08e2e31cb437d6382>:0
    at MonoBehaviourExtensions+<InvokingWhen>d__6.MoveNext () [0x0003d] in <1f3a14fdb43e46a08e2e31cb437d6382>:0
    at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00026] in <2cd8a4611dae40c2b95bf6c7f9c16416>:0
  2. unity_EF8057F78E5F0E6BB1D2


    Unity Technologies

    Jun 25, 2021
    Could you provide more details on how you're getting UnityServices.InitializeAsync() initialized? A code fragment would be useful.

    Also, it would be helpful if you can provide some details about projectID/EnvironmentID to see if there's any issues on the server end.