Unity Mirror - networked game object not showing up

765 Views Asked by At

I'm using Unity Mirror for networking my app so that a central server (not host) can send commands to the clients it is connected to. On play, the "game" will make the program a client or server automatically on play, so i don't have to use the Client/Server only buttons provided by the NetworkManagerHUD.

Currently I'm facing 2 problems:

  1. Client disconnects right after a connection with server is made. When I override the OnClientConnect function, I put the line base.OnClientConnect(conn). After stepping into the original function, I conclude it is the autoCreatePlayer set to true that is causing this problem. (the server and client are two instances of the program running on the same computer as I can only test using localhost).
public override void OnClientConnect(NetworkConnection conn)
    {

        base.OnClientConnect(conn);    //This line causes the error message 
        clientConnected = true;
        GameObject[] prefabs = Resources.LoadAll<GameObject>("NetworkingComponents");

        foreach (var prefab in prefabs)
        {
 
            NetworkClient.RegisterPrefab(prefab);

        }

        
        GameObject[] gos = Resources.LoadAll<GameObject>("NetworkingComponents");

    }

KCP received disconnect message

  1. Perhaps the most critical issue. Referring to the previous problem, if i did remove the line
    base.OnClientConnect(conn), client can connect, but all networked gameobjects (with NetworkIdentity) are still not showing up when connected as client, even though the NetworkManagerHUD says the program is connected as client. (Strangely, they are showing up if connected as Server.)

Here is the rest of the overriden NetworkManager code.

public class MyNetworkManager : NetworkManager
{

    public GameObject dropdown;
    public Canvas canvas;

    
    //---------------------------Networking stuff----------------------------------
    
    public List<NetworkNode> networkedNodes { get; } = new List<NetworkNode>();
    public List<Settings> networkedSettings { get; } = new List<Settings>();
    public List<NetworkedVisualisersDisplay> visualisersDisplays { get; } = new List<NetworkedVisualisersDisplay>();
    public List<Visualiser> visualisers{ get; } = new List<Visualiser>();
    public static MyNetworkManager instance = null;
    public NetworkedVisualisersDisplay visDisplayPrefab;
    public NetworkNode networkNode;
    private string homeName;
    public volatile bool clientConnected = false;

    public bool IsClientConnected()
    {
        return clientConnected;
    }
    //the purpose of having a delay is that we need to determine if the call to StartClient() actually started the player as a client. It could fail if it’s the first player instance on the network. 
    public IEnumerator DelayedStart()
    {
        //base.Start();
        StartClient();
        yield return new WaitForSeconds(2);
        print("conn count " + NetworkServer.connections.Count);

        if (!IsClientConnected())
        {
            NetworkClient.Disconnect();
         print(“starting as server”);
            StartServer();
            clientConnected = false;

        }
        else
        {
            print("starting as client");
        }
        
        visDisplayPrefab = Resources.Load<NetworkedVisualisersDisplay>("NetworkingComponents/NetworkedVisualisersDisplay");
        if (instance == null)
        {
            instance = this;
            print("instance = " + this);

        }
        else
        {
            print("manager destroyed");

            Destroy(gameObject);
        }
        yield return null;
    }


    //-----------------------------------------------------------------------------
    public override void Start(){

        StartCoroutine(DelayedStart());
  
    } 
    public override void OnStartServer()
    {
        
        GameObject[] prefabs = Resources.LoadAll<GameObject>("NetworkingComponents"); 
        foreach (var prefab in prefabs)
        {
            
            spawnPrefabs.Add(prefab);
                    
        }
        
    }
    
   
    public override void OnServerChangeScene(string scenename)
    {

        if (scenename.Equals("Visualisers"))
        {

            for (int i = 0; i < visualisersDisplays.Count; i++)
            {
                var conn = networkedNodes[i].connectionToClient;

                NetworkedVisualisersDisplay visSceneInstance = Instantiate(visualisersDisplays[i]);
                NetworkServer.Destroy(conn.identity.gameObject);
                NetworkServer.ReplacePlayerForConnection(conn, visSceneInstance.gameObject);
            }



        }
        else if (Settings.Instance.sceneNames.Contains(scenename))
        {
            for (int i = 0; i < visualisersDisplays.Count; i++)
            {
                var conn = visualisers[i].connectionToClient;
                var visInstance = Instantiate(visualisers[i]);
                NetworkServer.Destroy(conn.identity.gameObject);
                NetworkServer.ReplacePlayerForConnection(conn, visInstance.gameObject);
                
            }
        }
        

    }
    public override void OnServerAddPlayer(NetworkConnection conn)
    {       
        NetworkNode n = Instantiate(networkNode);
        NetworkServer.AddPlayerForConnection(conn, n.gameObject);
        NetworkNode.instance.DisplayMessage();
    }

   
    public override void OnClientConnect(NetworkConnection conn)
    {

        base.OnClientConnect(conn);
    //we are connected as a client
        clientConnected = true;
        GameObject[] prefabs = Resources.LoadAll<GameObject>("NetworkingComponents");

        foreach (var prefab in prefabs)
        {
            NetworkClient.RegisterPrefab(prefab);

        }
        
    }

}

Any help will be greatly appreciated!

0

There are 0 best solutions below