Search Unity

Synchronization of two int variables in Multiplayer UNet

Discussion in 'UNet' started by CocoJambo1, Apr 16, 2020.

  1. CocoJambo1

    CocoJambo1

    Joined:
    Apr 15, 2020
    Posts:
    1
    Dear Forum,

    I started programming a small game. It basically is an advanced version of 'Roll a Ball'.
    I implemented the Multiplayer mode but I very much struggle to keep two int variables count1 and count2 synchronized.

    The idea is to have collectibles for both players and increase the counter of the player who collected the collectible. How can I do that in a synchronized way? I already tried [SyncVar], [Command] and [ClientRpc] and its not working.

    Everytime I collect a collectible on either player, only the corresponding counter is updated for the player who collected. It should also show the score at the other player's screen.

    I would be very thankful for any help.

    Best regards
    Luca

    PS: Here is the code


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7.  
    8. public class PlayerController : NetworkBehaviour
    9. {
    10.     Text txt;
    11.     public float jumpheight;
    12.     public Text countText;
    13.     public Text countText1;
    14.     public Text countText2;
    15.     public Text winText;
    16.     public float speed;
    17.  
    18.     private bool isGrounded;
    19.     private Rigidbody rb;
    20.  
    21.     private int Winlimit = 8;
    22.     private bool Together=false;
    23.     private NetworkIdentity objNetId;
    24.  
    25.     private bool powerupfly;
    26.     private bool powerupslow;
    27.     private bool powerupspeed;
    28.  
    29.     [SyncVar]
    30.     private int count;
    31.     [SyncVar]
    32.     private int count1;
    33.     [SyncVar]
    34.     private int count2;
    35.  
    36.  
    37.  
    38.  
    39.  
    40. void Update() {
    41.  
    42.     if (!isLocalPlayer)
    43.         {
    44.           return;
    45.         }
    46.  
    47.  
    48.     void FixedUpdate()
    49.     {
    50.      
    51.         if (!isLocalPlayer)
    52.         {
    53.         return;
    54.         }
    55.    
    56.     float moveHorizontal = Input.GetAxis("Horizontal");
    57.     float moveVertical = Input.GetAxis("Vertical");
    58.     float moveJump = Input.GetAxis("Jump");
    59.  
    60.                
    61.  
    62.     }
    63.  
    64. void OnTriggerEnter(Collider other)
    65.     {
    66.  
    67.      if (!isLocalPlayer)
    68.         {
    69.           return;
    70.         }
    71.         if (Together)
    72.         {
    73.             if (other.gameObject.CompareTag("Pick Up_Static"))
    74.             {
    75.                 CmdDeactivateObject(other.gameObject);
    76.                 CmdIncreaseCount(1);
    77.                 SetCountText();
    78.             }
    79.             if (other.gameObject.CompareTag("Pick Up_Rotating"))
    80.             {
    81.                 CmdDeactivateObject(other.gameObject);
    82.                 CmdIncreaseCount(2);
    83.                 SetCountText();
    84.             }
    85.             if (other.gameObject.CompareTag("Pick Up_Random"))
    86.             {
    87.                 CmdDeactivateObject(other.gameObject);
    88.                 CmdIncreaseCount(2);
    89.                 SetCountText();
    90.             }
    91.             if (other.gameObject.CompareTag("Pick Up_Appearing"))
    92.             {
    93.                 CmdDestroyObject(other.gameObject);
    94.                 CmdIncreaseCount(2);
    95.                 SetCountText();
    96.             }
    97.         }
    98.         if (!Together)
    99.         {
    100.             if (isServer)
    101.             {
    102.                 if (other.gameObject.CompareTag("Pick Up_Static"))
    103.                 {
    104.                     CmdDeactivateObject(other.gameObject);
    105.                     CmdIncreaseCount1(1);
    106.                     SetCount1Text();
    107.                 }
    108.                 if (other.gameObject.CompareTag("Pick Up_Rotating"))
    109.                 {
    110.                     CmdDeactivateObject(other.gameObject);
    111.                     CmdIncreaseCount1(2);
    112.                     SetCount1Text();
    113.                 }
    114.                 if (other.gameObject.CompareTag("Pick Up_Random"))
    115.                 {
    116.                     CmdDeactivateObject(other.gameObject);
    117.                     CmdIncreaseCount1(2);
    118.                     SetCount1Text();
    119.                 }
    120.                 if (other.gameObject.CompareTag("Pick Up_Appearing"))
    121.                 {
    122.                     CmdDestroyObject(other.gameObject);
    123.                     CmdIncreaseCount1(2);
    124.                     SetCount1Text();
    125.                 }
    126.             }
    127.  
    128.             if (!isServer)
    129.             {
    130.                 if (other.gameObject.CompareTag("Pick Up_Static"))
    131.                 {
    132.                     CmdDeactivateObject(other.gameObject);
    133.                     CmdIncreaseCount2(1);
    134.                     SetCount2Text();
    135.                 }
    136.                 if (other.gameObject.CompareTag("Pick Up_Rotating"))
    137.                 {
    138.                     CmdDeactivateObject(other.gameObject);
    139.                     CmdIncreaseCount2(2);
    140.                     SetCount2Text();
    141.                 }
    142.                 if (other.gameObject.CompareTag("Pick Up_Random"))
    143.                 {
    144.                     CmdDeactivateObject(other.gameObject);
    145.                     CmdIncreaseCount2(2);
    146.                     SetCount2Text();
    147.                 }
    148.                 if (other.gameObject.CompareTag("Pick Up_Appearing"))
    149.                 {
    150.                     CmdDestroyObject(other.gameObject);
    151.                     CmdIncreaseCount2(2);
    152.                     SetCount2Text();
    153.                 }
    154.             }
    155.         }
    156.  
    157.      
    158.     }
    159.  
    160.    void SetCountText()
    161.     {      
    162.             //CmdChangeCount();
    163.             CountTextScript.score = count;
    164.             WinTextScript.score = count;
    165.        
    166.     }
    167.  
    168.     void SetCount1Text()
    169.     {
    170.         string info = count.ToString() +","+ count1.ToString() +","+ count2.ToString();
    171.         Debug.Log(info);
    172.         CountTextScript.score1 = count1;
    173.         CountTextScript.score2 = count2;
    174.         WinTextScript.score1 = count1;
    175.         WinTextScript.Serverplayer = true;    
    176.     }
    177.  
    178.     void SetCount2Text()
    179.     {
    180.         string info = count.ToString() +","+ count1.ToString() +","+ count2.ToString();
    181.         Debug.Log(info);
    182.         CountTextScript.score1 = count1;
    183.         CountTextScript.score2 = count2;
    184.         WinTextScript.score2 = count2;
    185.         WinTextScript.Serverplayer = false;
    186.     }
    187.  
    188.  
    189.     [ClientRpc]
    190.      void RpcPaintSlow()
    191.      {
    192.  
    193.                 Material newMat1 = Resources.Load("Materials/Powerup Slow", typeof(Material)) as Material;
    194.                 gameObject.GetComponent<Renderer>().material = newMat1;
    195.            
    196.     }
    197.  
    198.      [Command]
    199.      void CmdPaintSlow() {
    200.          objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    201.          objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    202.          RpcPaintSlow ();                                    // usse a Client RPC function to "paint" the object on all clients
    203.          //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    204.      }
    205.  
    206.      [ClientRpc]
    207.      void RpcPaintSpeed()
    208.      {
    209.  
    210.                 Material newMat1 = Resources.Load("Materials/Powerup Speed", typeof(Material)) as Material;
    211.                 gameObject.GetComponent<Renderer>().material = newMat1;
    212.            
    213.     }
    214.  
    215.      [Command]
    216.      void CmdPaintSpeed() {
    217.          objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    218.          objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    219.          RpcPaintSpeed ();                                    // usse a Client RPC function to "paint" the object on all clients
    220.          //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    221.      }
    222.  
    223.      [ClientRpc]
    224.      void RpcPaintFly()
    225.      {
    226.  
    227.                 Material newMat1 = Resources.Load("Materials/Powerup Fly", typeof(Material)) as Material;
    228.                 gameObject.GetComponent<Renderer>().material = newMat1;
    229.            
    230.     }
    231.  
    232.      [Command]
    233.      void CmdPaintFly() {
    234.          objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    235.          objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    236.          RpcPaintFly ();                                    // usse a Client RPC function to "paint" the object on all clients
    237.          //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    238.      }
    239.  
    240.      [ClientRpc]
    241.      void RpcPaintSuper()
    242.      {
    243.  
    244.                 Material newMat1 = Resources.Load("Materials/Super Power", typeof(Material)) as Material;
    245.                 gameObject.GetComponent<Renderer>().material = newMat1;
    246.            
    247.     }
    248.  
    249.      [Command]
    250.      void CmdPaintSuper() {
    251.          objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    252.          objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    253.          RpcPaintSuper ();                                    // usse a Client RPC function to "paint" the object on all clients
    254.          //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    255.      }
    256.  
    257.  
    258.      [ClientRpc]
    259.      void RpcPaintPlayer1()
    260.      {
    261.  
    262.                 Material newMat1 = Resources.Load("Materials/Player 1", typeof(Material)) as Material;
    263.                 gameObject.GetComponent<Renderer>().material = newMat1;
    264.            
    265.     }
    266.  
    267.      [Command]
    268.      void CmdPaintPlayer1() {
    269.          objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    270.          objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    271.          RpcPaintPlayer1 ();                                    // usse a Client RPC function to "paint" the object on all clients
    272.          //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    273.      }
    274.  
    275.      [ClientRpc]
    276.      void RpcPaintPlayer2()
    277.      {
    278.  
    279.                 Material newMat1 = Resources.Load("Materials/Player 2", typeof(Material)) as Material;
    280.                 gameObject.GetComponent<Renderer>().material = newMat1;
    281.            
    282.     }
    283.  
    284.      [Command]
    285.      void CmdPaintPlayer2() {
    286.          objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    287.          objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    288.          RpcPaintPlayer2 ();                                    // usse a Client RPC function to "paint" the object on all clients
    289.          //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    290.      }
    291.  
    292.      [ClientRpc]
    293.      void RpcDeactivateObject(GameObject GO)
    294.      {
    295.             GO.SetActive(false);
    296.            
    297.     }
    298.  
    299.      [Command]
    300.      void CmdDeactivateObject(GameObject GO) {
    301.          objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    302.          objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    303.          RpcDeactivateObject(GO);                                    // usse a Client RPC function to "paint" the object on all clients
    304.          //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    305.      }
    306.  
    307.      [ClientRpc]
    308.      void RpcDestroyObject(GameObject GO)
    309.      {
    310.        
    311.             Destroy(GO);
    312.            
    313.     }
    314.  
    315.      [Command]
    316.      void CmdDestroyObject(GameObject GO) {
    317.          objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    318.          objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    319.          RpcDeactivateObject(GO);                                    // usse a Client RPC function to "paint" the object on all clients
    320.          //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    321.      }
    322.  
    323.  
    324.  
    325.  
    326.     [ClientRpc]
    327.         void RpcIncreaseCount(int valnum)
    328.         {
    329.             count = count +valnum;
    330.         }
    331.     [Command]
    332.         void CmdIncreaseCount(int valnum) {
    333.             objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    334.             objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    335.             RpcIncreaseCount(valnum);                                    // usse a Client RPC function to "paint" the object on all clients
    336.             //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    337.         }
    338.  
    339.  
    340.         [ClientRpc]
    341.         void RpcIncreaseCount1(int valnum)
    342.         {
    343.             count1 = count1 +valnum;
    344.         }
    345.     [Command]
    346.         void CmdIncreaseCount1(int valnum) {
    347.             objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    348.             objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    349.             RpcIncreaseCount1(valnum);                                    // usse a Client RPC function to "paint" the object on all clients
    350.             //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    351.         }
    352.  
    353.  
    354.         [ClientRpc]
    355.         void RpcIncreaseCount2(int valnum)
    356.         {
    357.             count2 = count2 +valnum;
    358.         }
    359.         [Command]
    360.         void CmdIncreaseCount2(int valnum) {
    361.             objNetId = gameObject.GetComponent<NetworkIdentity> ();        // get the object's network ID
    362.             objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
    363.             RpcIncreaseCount2(valnum);                                    // usse a Client RPC function to "paint" the object on all clients
    364.             //objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
    365.         }
    366.  
    367. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm having trouble understanding what you're trying to do in your OnTriggerEnter. You're returning if not isLocalPlayer, but later your're checking isServer. isLocalPlayer and isServer will only be true for the Player GameObject representing the player on the server, but then you're using that to send Commands. Why? There's no reason to send Commands to the server when you already know you're the server.

    You also need to verify in your Commands that sending a reference to other.gameObject is even working. I've seen problems with this when using Unet (I completely abandoned doing so entirely because I found the feature to be fundamentally broken, but YMMV). Unet was abandoned in a buggy state.

    But basically add Debug.Log statements everywhere and figure out where things are breaking down.