Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by ankit0710 · Sep 06, 2015 at 10:25 AM · errornetworkingconnectionnetwork instantiate

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Trouble with LLAPI 0 Answers

uNet- A connection has already been set as ready. There can only be one. 3 Answers

【PUN2】I want to disable the instantiation of an object in PhotonView at a specified time. 1 Answer

NetworkServer.Spawn Deleting Objects 2 Answers

The magic key to networking? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges