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().isReady == true && Players[1].GetComponent().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:
public class CarControler : MonoBehaviour {
void Start ()
{
thisPlayer = GetComponent<Player>();
}
if(Input.GetKey(KeyCode.Q) == true)
{
thisPlayer.isReady = true;
}
}
Player script:
public class Player : NetworkBehaviour {
[SyncVar]
public bool isReady = false;
}
Script that should check if both are true:
public class RaceStart : NetworkBehaviour {
void Start ()
{
Players = GameManager.GetAllPlayers(); // this gets all of the players in a array
}
void Update ()
{
if (Players[0].isReady == true && Players[1].isReady == true) { //start game
}
}
}