Search Unity

Trying to sync SyncVars across all prefabs

Discussion in 'Multiplayer' started by MaileyMan, Jul 27, 2019.

  1. MaileyMan

    MaileyMan

    Joined:
    Jul 25, 2019
    Posts:
    7
    Hi everyone. I'm making a test multiplayer game which randomly generates two numbers on the host, then tells the client about one of these numbers so that it can instantiate a card from a deck. The problem I am having is that for each player prefab that spawns in (two, in this case) a new pair of random numbers is generated, meaning the client is referencing a different number than what the host generated. This is happening in spite of the fact that the random number generation is inside an if statement which can only be executed on the server.

    So in short I have two questions:
    • Why is the client generating a pair of numbers?
    • How can I make the client reference the SyncVars in the host prefab?

    The script on the player prefabs I am using is as follows.

    Code (CSharp):
    1.     [SyncVar]
    2.     public int hostCard;
    3.  
    4.     [SyncVar]
    5.     public int clientCard;
    6.  
    7.     public static bool sent = false;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         if (isServer)
    12.         {
    13.             hostCard = Random.Range(0, 52);
    14.             clientCard = Random.Range(0, 52);
    15.         }
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update () {
    20.         if (isServer)
    21.         {
    22.             GameplayTest.card = hostCard + 1;
    23.         }
    24.  
    25.         if (isClient)
    26.         {
    27.             GameplayTest.card = clientCard + 1;
    28.         }
    29.  
    30.         sent = true;
    31.         GameplayTest.received = true;
    32.     }
    33. }


    Any help is much appreciated!
     
    Last edited: Jul 27, 2019