Https://www.gamedev.tv/courses/unity-multiplayer-netcode/lectures/46684557

Good Evening Everyone today I am facing this issues, but when I check those Scripts well both the Scripts of ClientSingleton & HostSingleton are same as the Tutor wrote, but still I am facing this Error :-
"You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor ()
HostGameManager:.ctor ()
HostSingleton:CreateHost () (at Assets/Scripts/Networking/Host/HostSingleton.cs:34)
ApplicationController/d__3:MoveNext () (at Assets/Scripts/Networking/ApplicationController.cs:34)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start<ApplicationController/d__3> (ApplicationController/d__3&)
ApplicationController:LaunchInMode (bool)
ApplicationController/d__2:MoveNext () (at Assets/Scripts/Networking/ApplicationController.cs:14)
System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start<ApplicationController/d__2> (ApplicationController/d__2&)
ApplicationController:Start ()

The error is from HostSingleton, but the problem is not there. The problem is this

It appears your HostGameManager inherits from MonoBehaviour - which it shouldn’t. The HostGameManager is just a class, not a MonoBehaviour

1 Like

Is there Any way to get Subtitles !!??!! please,
because English is not my 1st Language,

well it seems even after fixing those issue I am still facing problem


//Well here this Image represents of Build Settings , where I have saved in the same sequences.


// the script


On Build & Run when I stopped I found this error , saying " Unable to find player assembly: Library/PlayerScriptAssemblies/UnityEngine.TestRunner.dll "


OnPlayTest still no warning /errors


still no error

That’s not an error, it’s a warning. Check all your scripts and remove all unused using from them because I think one of them may be referencing the test framework.

You can remove it easy by right-clicking at the top where they are and selecting 'Remove unused usings`. I’m not sure what the exact text would be in VS Code

I did, but still Its not switching to the “Menu” page .
Still same Error/Warning
:worried:

Well, you said you had an error. You didn’t say it wasn’t switching to the menu. Paste all your networking scripts so we can look at it. Don’t paste screenshots of the code, please. See here:

1 Like

This is the one for ClientGameManager

using System.Threading.Tasks;
using Unity.Services.Core;
using UnityEngine.SceneManagement;

public class ClientGameManager
{
    private const string MenuSceneName = "Menu";

   public async Task<bool> InitAsync()
    {
        // Atuthenticate player
        await UnityServices.InitializeAsync();

        AuthState authState = await AuthenticationWrapper.DoAuth();

        if(authState == AuthState.Authenticated)
        {
            return true;
        }
        return false;


    }

    public void GoToMenu()
    {
        SceneManager.LoadScene(MenuSceneName);
    }



}

AuthenticationWrapper:-

using System.Threading.Tasks;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;

public static class AuthenticationWrapper 
{
    public static AuthState AuthState { get; private set; } = AuthState.NotAuthenticated;

    public static async Task<AuthState> DoAuth(int maxRetries = 5)
    {
        if(AuthState == AuthState.NotAuthenticated)
        {
            return AuthState;
        }

        if(AuthState == AuthState.Authenticating)
        {
            Debug.LogWarning("Already Authenticating!");
            await Authenticating();
            return AuthState;
        }

        await SignInAnonymouslyAsync(maxRetries);

        return AuthState;
    }


    private static async Task<AuthState> Authenticating()
    {
        while(AuthState == AuthState.Authenticating || AuthState == AuthState.NotAuthenticated)
        {
            await Task.Delay(200);
        }

        return AuthState;

    }


    private static async Task SignInAnonymouslyAsync(int maxRetries)
    {
        AuthState = AuthState.Authenticating;

        int retries = 0;
        while (AuthState == AuthState.Authenticating && retries < maxRetries)
        {
            try
            {
                await AuthenticationService.Instance.SignInAnonymouslyAsync();

                if (AuthenticationService.Instance.IsSignedIn && AuthenticationService.Instance.IsAuthorized)
                {
                    AuthState = AuthState.Authenticated;
                    break;
                }
            }
            catch(AuthenticationException ex) //if failed on Autheticate
            {
                Debug.LogError(ex);
                AuthState = AuthState.Error;

            }
            catch(RequestFailedException exception) //if no internet connection found
            {
                Debug.LogError(exception);
                AuthState = AuthState.Error;

            }
            

            retries++;
            await Task.Delay(1000);

        }
        if(AuthState != AuthState.Authenticated)
        {
            Debug.LogError($"Player was not signed in Successfully after { retries} retries");
            AuthState = AuthState.TimeOut;
        }
    }

}

//to check the status of Authentication
public enum AuthState
{
    NotAuthenticated,
    Authenticating,
    Authenticated,
    Error,
    TimeOut
}

ApplicationController : -

using System.Threading.Tasks;
using UnityEngine;

public class ApplicationController : MonoBehaviour
{
    [SerializeField] private ClientSingleton clientPrefab;
    [SerializeField] private HostSingleton hostPrefab;
    private async void Start()
    {
        DontDestroyOnLoad(gameObject);

       await LaunchInMode(SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Null);

    }

    private async Task LaunchInMode(bool isDedicatedServer)
    {
        if(isDedicatedServer)
        {


        }
        else
        {
          ClientSingleton clientSingleton= Instantiate(clientPrefab);

         bool authenticated =  await clientSingleton.CreateClient();


            HostSingleton hostSingleton = Instantiate(hostPrefab);

            hostSingleton.CreateHost();
            //Go to main Menu
            if(authenticated)
            {
                clientSingleton.GameManager.GoToMenu();
            }
        }
    }


}

ClientNetworkTransform:-

using Unity.Netcode.Components;

public class ClientNetworkTransform : NetworkTransform
{
    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();
        CanCommitToTransform = IsOwner;
    }

    protected override void Update()
    {
        CanCommitToTransform = IsOwner;
        base.Update();
        if (NetworkManager != null)
        {
            if(NetworkManager.IsConnectedClient || NetworkManager.IsListening)
            {
                if (CanCommitToTransform)
                {
                    TryCommitTransformToServer(transform, NetworkManager.LocalTime.Time);
                }
            }

        }


    }
    protected override bool OnIsServerAuthoritative()
    {
        return false;
    }

   



}

ClientSingelton::–

using System.Threading.Tasks;
using UnityEngine;

public class ClientSingleton : MonoBehaviour
{

    private static ClientSingleton instance;

    public ClientGameManager GameManager {get; private set;}

    public static ClientSingleton Instance
    {
        get
        {
            if (instance != null) { return instance; }

            instance = FindObjectOfType<ClientSingleton>();

            if (instance == null) { Debug.LogError("No ClientSingleton in the scene!"); return null; }

            return instance;
        }
    } 

    private void Start()
    {
        DontDestroyOnLoad(gameObject);
    }

    public async Task<bool> CreateClient()
    {
        GameManager  = new ClientGameManager();

        return await GameManager.InitAsync();
    }


}

HostGameManager: –

/*no libraries used ( USING ... not used here as the System said Remove unnecessary using directives (IDE0005) " */
public class HostGameManager 
{
   /*public async Task InitAsync()
    {
        // Atuthenticate player
    }
   */
   


   
}


HostSingleton Script :–

using UnityEngine;

public class HostSingleton : MonoBehaviour
{

    private static HostSingleton instance;

    private HostGameManager gameManager;

    public static HostSingleton Instance
    {
        get
        {
            if (instance != null) { return instance; }

            instance = FindObjectOfType<HostSingleton>();

            if (instance == null) { Debug.LogError("No HostSingleton in the scene!"); return null; }

            return instance;
        }
    } 

    private void Start()
    {
        DontDestroyOnLoad(gameObject);
    }

    public void  CreateHost()
    {
        gameManager  = new HostGameManager();

        //await gameManager.InitAsync();
    }


}

JoinServer Script :-

using Unity.Netcode;
using UnityEngine;

public class JoinServer : MonoBehaviour
{
    public void StartHost()
    {
        NetworkManager.Singleton.StartHost();
    }
   public void StartClient()
    {
        NetworkManager.Singleton.StartClient();
    }
}

I think the problem is here. This should not be NotAuthenticated, it should be Authenticated. You start NotAuthenticated which means this will just exit the method and you will never become authenticated. If you’re not authenticated, the game does not go to the next scene - your Menu

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms