- Home /
A connection has already been set as ready. There can only be one. Error received from NetworkIdentity:UNetStaticUpdate
I am getting this error on the client. I get it as soon I the JoinMatch request results in success. I am using matchmaker services to host and join. The way my code works is I do not use auto create player in the network manager. Also I am overriding the OnServerReady callback so I can check for if both the players have connected . If so I start the game.
I have a custom NetworkManager Script. I have a GameController Object which is a Network Behaviour. It has the Network Identity component attached and is registered as spawnable with NetworkManager. This object gets instantiated on the server and a Network.Spawn is called for it when both the players have connected indicating the game can be started. This is done in a custom function called from OnServerReady when the number of connections reach 2. On the server, it doesn't seem to be throwing any error or warnings.
The GameController is responsible for adding player for the clients or replacing them. It is turn-based, so the player is not added for the client on either ready or connect callbacks. A player is added on start of the game for client on host and when its turn ends, the player for remote client gets added.
I get the error right as you join the match. I am using the matchmaker service for this. I can't figure out what seems to be causing this. I understand there are multiple calls happening to NetworkServer.SetClientReady(). But I have tried to debug and it seems to be calling just once for each client (local and remote). Moreover that is done only on the server. So no chance of that causing the error on Client. I am pasting the code of both my custom NetworkManager as well the GameController scripts. Please let me know if you have any idea as to how I may solve this.
public class NetMan : NetworkManager {
public const short CustomAddPlayerMsgType = MsgType.Highest + 1;
public const short CustomReplacePlayerMsgType = MsgType.Highest + 2;
public class CustomAddReplacePlayerMsg : MessageBase {
public int connectionId;
public NetworkHash128 assetId;
}
short numberOfPlayersReady = 0;
public bool isHost;
public MatchDesc matchToJoin;
// Use this for initialization
void Start () {
PlayerPrefs.SetString ("CloudNetworkingId", "257551");
StartMatchMaker();
matchMaker.SetProgramAppID ((AppID)257551);
NetworkServer.RegisterHandler(CustomAddPlayerMsgType, OnServerCustomAddPlayer);
NetworkServer.RegisterHandler(CustomReplacePlayerMsgType, OnServerCustomReplacePlayer);
}
// Update is called once per frame
void Update () {
}
public void OnServerCustomAddPlayer (NetworkMessage msg) {
CustomAddReplacePlayerMsg decodedMsg = msg.ReadMessage<CustomAddReplacePlayerMsg>();
FindObjectOfType<GameController>().CmdAddPlayerObj(decodedMsg.connectionId, decodedMsg.assetId);
}
public void OnServerCustomReplacePlayer (NetworkMessage msg) {
CustomAddReplacePlayerMsg decodedMsg = msg.ReadMessage<CustomAddReplacePlayerMsg>();
FindObjectOfType<GameController>().CmdReplacePlayerObj(decodedMsg.connectionId, decodedMsg.assetId);
}
public override void OnServerReady (NetworkConnection conn) {
if (numberOfPlayersReady == 2) {
for (int i = 0; i < NetworkServer.connections.Count; i++) {
if (NetworkServer.connections[i] == conn) {
NetworkServer.connections[i].Disconnect();
return;
}
}
}
NetworkServer.SetClientReady(conn);
numberOfPlayersReady++;
if (numberOfPlayersReady == 2) {
StartGame();
}
}
void StartGame () {
GameObject controllerObj = (GameObject) Instantiate(spawnPrefabs[1]);
NetworkServer.Spawn(controllerObj);
controllerObj.GetComponent<GameController>().StartTheGame();
}
}
public class GameController : NetworkBehaviour {
[SyncVar(hook = "OnNbrOfTurnsRmngChanged")]
short numberOfTurnsRemainingInGame = 6;
private void OnNbrOfTurnsRmngChanged (short newNbr) {
Debug.Log("Nbr Changed to " + newNbr);
numberOfTurnsRemainingInGame = newNbr;
}
[SyncVar(hook = "OnHostTurnChanged")]
bool hostTurn = true;
private void OnHostTurnChanged (bool changedTurn) {
Debug.Log("Value changed to " + changedTurn);
hostTurn = changedTurn;
}
NetMan netManInstance;
// Use this for initialization
void Awake () {
netManInstance = GameObject.Find("Manager").GetComponent<NetMan>();
}
void Start () {
}
// Update is called once per frame
void Update () {
}
[Server]
public void StartTheGame () {
numberOfTurnsRemainingInGame = 6;
hostTurn = true;
StartTurn();
}
[Server]
void StartTurn () {
if (numberOfTurnsRemainingInGame > 0) {
RpcStartMyTurn(hostTurn);
} else {
EndTheGame();
}
}
[Server]
void SwitchTurn () {
hostTurn = !hostTurn;
StartTurn();
}
[Server]
void EndTheGame () {
Debug.Log("Game Finished");
}
[Client]
public void MyTurnFinished () {
CmdOnPlayerTurnEnd();
}
[Command]
void CmdOnPlayerTurnEnd () {
numberOfTurnsRemainingInGame--;
SwitchTurn();
}
[Command]
public void CmdAddPlayerObj (int connectionId, NetworkHash128 assetId) {
List<NetworkConnection>listOfAllConnections = NetworkServer.connections;
listOfAllConnections.AddRange(NetworkServer.localConnections);
foreach (GameObject gameObject in netManInstance.spawnPrefabs) {
if (gameObject.GetComponent<NetworkIdentity>().assetId.Equals(assetId)) {
foreach (NetworkConnection conn in listOfAllConnections) {
if (conn == null) {
continue;
} else {
if (conn.connectionId == connectionId) {
GameObject playerObj = (GameObject)Instantiate(gameObject);
NetworkServer.AddPlayerForConnection(conn, playerObj, 0);
playerObj.GetComponent<PlayerNetworkSetup>().RpcActivatePlayer();
break;
}
}
}
break;
}
}
}
[Command]
public void CmdReplacePlayerObj (int connectionId, NetworkHash128 assetId) {
foreach (GameObject gameObject in netManInstance.spawnPrefabs) {
if (gameObject.GetComponent<NetworkIdentity>().assetId.Equals(assetId)) {
foreach (NetworkClient client in NetworkClient.allClients) {
if (client.connection.connectionId == connectionId) {
GameObject playerObj = (GameObject)Instantiate(gameObject);
NetworkServer.ReplacePlayerForConnection(client.connection, playerObj, 0);
playerObj.GetComponent<PlayerNetworkSetup>().RpcActivatePlayer();
break;
}
}
break;
}
}
}
[ClientRpc]
void RpcStartMyTurn (bool turnOfHost) {
if ((turnOfHost && netManInstance.isHost) || (!turnOfHost && !netManInstance.isHost)) {
if (netManInstance.client.connection.playerControllers.Count == 0) {
NetMan.CustomAddReplacePlayerMsg msg = new NetMan.CustomAddReplacePlayerMsg();
msg.connectionId = netManInstance.client.connection.connectionId;
msg.assetId = netManInstance.playerPrefab.GetComponent<NetworkIdentity>().assetId;
netManInstance.client.Send(NetMan.CustomAddPlayerMsgType, msg);
//CmdAddPlayerObj(netManInstance.client.connection.connectionId, netManInstance.playerPrefab.GetComponent<NetworkIdentity>().assetId);
} else {
NetMan.CustomAddReplacePlayerMsg msg = new NetMan.CustomAddReplacePlayerMsg();
msg.connectionId = netManInstance.client.connection.connectionId;
msg.assetId = netManInstance.playerPrefab.GetComponent<NetworkIdentity>().assetId;
netManInstance.client.Send(NetMan.CustomReplacePlayerMsgType, msg);
//CmdReplacePlayerObj(netManInstance.client.connection.connectionId, netManInstance.playerPrefab.GetComponent<NetworkIdentity>().assetId);
}
}
}
}
PS: Really sorry if I am not clear and for this very long description. My first time posting the question so not sure what to include.