2
votes

Here I am creating a multiplayer game with Unity Networking. What I want is to start the game when both Server and client is connected. In my game I have 2 players one will act as a server who hosts the game and the other acts as a client who connects to that host with the servers IPAddress. So when both are connected and ready to play i want to start the game. Can anyone help me how can i get to know that both are connected.

Here is the code what I am using.

public NetworkManager manager;

public void startServerr()
{
NetworkServer.Listen(9000);
    NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
    NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
    NetworkServer.RegisterHandler(MsgType.Error, OnError);
    manager.StartHost();
}

public void connectToServer()
{
    manager.networkAddress = PlayerPrefs.GetString("oppPlayerIP");
    manager.StartClient();
}

public void OnConnected(NetworkMessage netMsg)
{
    Debug.Log("Client Connected");
}

public void OnDisconnected(NetworkMessage netMsg)
{
    Debug.Log("Disconnected");
}

public void OnError(NetworkMessage netMsg)
{
    Debug.Log("Error while connecting");
}
2

2 Answers

3
votes

You are currently using NetworkManager... If you prefer to use NetworkManager over NetworkServer and NetworkClient that requires manual registering of the events, then your script must inherit from NetworkManager.

Once you inherit from NetworkManager, you can then override and use OnClientConnect and OnServerConnect functions to check when client is connect to server and when server is connected to client respectively.

Here is an example:

public class NetTest : NetworkManager
{
    private NetworkManager manager;

    public void startServerr()
    {
        manager = this;
        manager.StartHost();
    }

    public void connectToServer()
    {
        //manager.networkAddress = PlayerPrefs.GetString("oppPlayerIP");
        manager.StartClient();
    }

    public override void OnClientConnect(NetworkConnection conn)
    {
        //base.OnClientConnect(conn);
    }

    public override void OnClientDisconnect(NetworkConnection conn)
    {
        //base.OnClientDisconnect(conn);
    }
    public override void OnServerConnect(NetworkConnection conn)
    {
        //base.OnServerConnect(conn);
    }

    public override void OnServerDisconnect(NetworkConnection conn)
    {
        //base.OnServerDisconnect(conn);
    }
}

You can find more network callback functions in the public function section here.

0
votes

First of I recommend you reading all the documentation about unity networking before actually trying to make a connection. After you've done that you should know how to handle this request. If you don't know by then here is how to handle this specific request.

First you need to let the server handle all incoming connections by using the method OnConnected(NetworkMessage netMsg). You should be able to recognize the individual players connected to the server. Now you only need to create a ready button and send the ready status towards the server when clicked. If every player connected has send his isReady message towards the server you can start the game. this is done by sending a message from the server towards all the connected clients.

Hope this helps, any questions just ask.