Search Unity

Syncing from client to server - server to client

Discussion in 'Multiplayer' started by DomasP, Dec 3, 2016.

  1. DomasP

    DomasP

    Joined:
    Aug 21, 2016
    Posts:
    12
    Hello, I'm trying to create a simple multiplayer game that starts after both of the player has clicked some button. I have a Player script that has a public bool isReady. The bool is set to true when the player presses the Q button. And I have another script that should start the game when both of the bools are true. In the script I have an array that has all of the players in it. And I check if both of the bools are true with this if statement:
    if(Players[0].GetComponent<Player>().isReady == true && Players[1].GetComponent<Player>().isReady == true) { //start game }
    The problem is that if the bools change on the local player only. For example: If I am Player1 and press the Q button the Player2's client wont have any way of knowing if Player1 pressed the button or not. If I mark the isReady bool as a SyncVar it only works if you are not the host. I think thats because the synchronization is done from the server to clients. So if I am Player1(host) and I press Q, Player2(client) will know that I pressed Q and that I'm ready. But If I am Player2(client) and press Q, Player1(host) will not see that I pressed Q. Can someone please explain how could I fix this problem? I would be very thankful :) . Heres a summary of my code:
    Code (CSharp):
    1. public class CarControler : MonoBehaviour {
    2.     void Start ()
    3.     {
    4.         thisPlayer = GetComponent<Player>();
    5.     }
    6.         if(Input.GetKey(KeyCode.Q) == true)
    7.         {
    8.             thisPlayer.isReady = true;
    9.         }
    10. }
    Player script:
    Code (CSharp):
    1. public class Player : NetworkBehaviour {
    2.  
    3.     [SyncVar]
    4.     public bool isReady = false;
    5. }
    Script that should check if both are true:
    Code (CSharp):
    1. public class RaceStart : NetworkBehaviour {
    2.     void Start ()
    3.     {
    4.         Players = GameManager.GetAllPlayers(); // this gets all of the players in a array
    5.     }
    6. void Update ()
    7.     {
    8. if (Players[0].isReady == true && Players[1].isReady == true) { //start game
    9. }
    10. }
    11. }
     
    Last edited: Dec 4, 2016
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    You are right, SyncVars mean 'sync from server to clients'. You can use Commands to send from a client to the server. Check the UNET manual for examples.
     
  3. DomasP

    DomasP

    Joined:
    Aug 21, 2016
    Posts:
    12
    Ok, so if I do a command:
    Code (CSharp):
    1.     [Command]
    2.     public void CmdReady()
    3.     {
    4.         isReady = true;
    5.     }
    This will only change the isReady on the server(host), if I also mark isReady as a SyncVar it should be the same for all no matter if host or client right? But this still doesnt work :/. On the client's screen it does, when the host presses Q and the Client presses Q the game begins, but on the Host's screen when the race doesnt start.
     
  4. DomasP

    DomasP

    Joined:
    Aug 21, 2016
    Posts:
    12
    Ok another noob problem :d. If I do what I said in the response above, something interesting happens. If I host a game from the unity editor, and join through a build, everything actually works the game starts on both screens. But if I host through the build and join through the editor the game only starts on the editor. How is that possible? If host and join through build's without using the editor it works on the client only.
     
    Last edited: Dec 4, 2016
  5. goneboy0623

    goneboy0623

    Joined:
    Jan 3, 2020
    Posts:
    2
    are both scenes in the build?