Search Unity

ClientRpc Not Working Properly, Only Sending To Some Players

Discussion in 'Multiplayer' started by warrencwwong, Jul 1, 2019.

  1. warrencwwong

    warrencwwong

    Joined:
    Aug 17, 2018
    Posts:
    25
    Hello, I am making a weapon switching system. Here is the most important part of the script:
    Code (CSharp):
    1.  
    2. void Start()
    3. {
    4.      if (isLocalPlayer)
    5.      {
    6.          foreach (Gun gun in AllGuns)
    7.          {
    8.              gun.AmmoLeft = gun.MagazineCapacity;
    9.              if (gun.selected && gun.unlocked)
    10.              {
    11.                  StartCoroutine(AddWeapon(gun));
    12.              }
    13.          }
    14.      }
    15. }
    16. IEnumerator AddWeapon(Gun gun)
    17. {
    18.      yield return new WaitForSeconds(0.5f);
    19.      CmdAddWeapon(gun.index);
    20.    
    21. }
    22.  
    23. void Update()
    24. {
    25.      if (isLocalPlayer)
    26.      {
    27.          reload.CurrentGun = currentGun;
    28.      }
    29.    
    30. }
    31. [Command]
    32. void CmdAddWeapon(int i)
    33. {
    34.    
    35.      RpcAddWeapon(i);
    36. }
    37. [ClientRpc]
    38. void RpcAddWeapon(int i)
    39. {
    40.    
    41.      Gun GunToAdd;
    42.      foreach(Gun gun in AllGuns)
    43.      {
    44.          if(gun.index == i)
    45.          {
    46.            
    47.              GunToAdd = gun;
    48.              Guns.Add(GunToAdd);
    49.          }
    50.        
    51.      }
    52.    
    53. }
    54. public void ChangeWeapon(int i)
    55. {
    56.    
    57.      CmdChangeWeapon(i);
    58. }
    59. [Command]
    60. void CmdChangeWeapon(int i)
    61. {
    62.    
    63.      RpcChangeWeapon(i);
    64. }
    65. [ClientRpc]
    66. void RpcChangeWeapon(int i)
    67. {
    68.    
    69.      Gun GunToChange;
    70.      foreach(Gun gun in Guns)
    71.      {
    72.          if(gun.index == i)
    73.          {
    74.            
    75.              GunToChange = gun;
    76.              ChangeWeaponbase(GunToChange);
    77.          }
    78.      }
    79.    
    80. }
    81. void ChangeWeaponbase(Gun gun)
    82. {
    83.          playerShoot.Currentgun = gun;
    84.          reload.Ammolefttext.text = gun.AmmoLeft.ToString();
    85.          reload.ReloadTime = gun.ReloadTime;
    86.          InstantiateGun(gun);
    87. }
    88. void InstantiateGun(Gun gun)
    89. {
    90.    
    91.      foreach (Transform weapon in WeaponHolder)
    92.      {
    93.          Destroy(weapon.gameObject);
    94.      }
    95.      GameObject go = Instantiate(gun.GunModel, WeaponHolder);
    96.      go.transform.position = new Vector3(WeaponHolder.position.x, WeaponHolder.position.y - gun.YMinusAmount, WeaponHolder.position.z);
    97.    
    98.      go.transform.localScale = gun.Scale;
    99.      go.transform.localEulerAngles = gun.rotation;
    100.      playerShoot.gunObject = go;
    101. }
    In this script, "Gun" is a scriptable object and the "selected" and "unlocked" fields are working fine. When the CmdAddWeapon function is called, it sends the gun's index to the server and back to all the clients through the RpcAddWeapon. The problem is: the host is able to see what guns the other clients have but the clients couldn't see what gun the host has! Why is that? Any help would be deeply appreciated. Thanks in advance.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are guns being added to the host player before some clients have connected? ClientRpc's are not cached, so any clients connected after a ClientRpc has been sent will never receive it. A SyncList doesn't have that limitation though.
     
  3. warrencwwong

    warrencwwong

    Joined:
    Aug 17, 2018
    Posts:
    25
    SyncLists don't work for the Guns List. I suppose it's a bug. And also, I have already added a delay to the command and clientrpc, so the clients should be set up already.

    EDIT: Oh! Sorry! I get what you are saying now. Thanks a lot.
     
    Last edited: Jul 3, 2019