Search Unity

UNET Showing Equipment (Host works/Client doesnt)

Discussion in 'UNet' started by KaidaStudios, Sep 10, 2017.

  1. KaidaStudios

    KaidaStudios

    Joined:
    Aug 4, 2017
    Posts:
    34
    The host correctly displays this information, the client works but does not display the information to the server. I am so confused now I feel I have tried everything. Here is my code please any help is appreciated!

    This is attached on Player:
    Code (CSharp):
    1.  [SyncVar(hook = "Rpcchangechest")]
    2.      public int echest;
    3.   [ClientRpc]
    4.      public void Rpcchangechest(int num) {
    5.          echest = num;
    6.          if (body.male) {
    7.              if (ChestName == ChestName) {
    8.                  Destroy (ChestInst);
    9.              }
    10.              body.Mesh_male_torso.active = false;
    11.              body.Mesh_male_arms.active = true;
    12.              ChestName = Chest [num].name;
    13.              GameObject NewChest = (GameObject)Instantiate (Resources.Load ("Armor/" + ChestName));
    14.              ChestInst = NewChest;
    15.              Destroy (NewChest);
    16.              AddChest ();
    17.          }
    18.      }
    This is from my UI Script:
    Code (CSharp):
    1.   if (itemInfo.EquipType == UIEquipmentType.Chest && !equipped)
    2.                  {
    3.                      //for calling on client
    4.                      transform.root.SendMessage("Rpcchangechest", itemInfo.ID);
    5. //Supposed to sync?
    6.                      Player.GetComponent<ChangeArmor>().echest = itemInfo.ID;
    7. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm a little confused. What do you mean by "does not display the information to the server"? Did you mean "from"?

    Also why are you calling a clientrpc from a syncvar hook? A clientrpc is something you call on the server to have it called on the client, but if you're calling it from a syncvar hook you're effectively trying to call it on the client from the client. You update the syncvar on the server (syncvars can only be updated on the server), the value gets updated on the client, which on the client then calls the clientrpc in the syncvar hook. Doesn't make sense.
     
    KaidaStudios likes this.
  3. KaidaStudios

    KaidaStudios

    Joined:
    Aug 4, 2017
    Posts:
    34
    Yes thank you! I have figured it out, this whole UNET thing has been a mission and I figured you have to have to first call [Command] and call [ClientRPC] from within the [Command] I was able to get everything working perfectly! Thank you for your response! For anyone else having this issues, this is how I solved it:

    Code (CSharp):
    1.  [SyncVar(hook = "changechest")]
    2.     public int echest;
    3.  
    4. //The First Method Called
    5.     public void updatechest(int newid)
    6.     {
    7.         if (!Network.isServer)
    8.         {
    9.             Cmdchangechest(newid);
    10.         } else
    11.         {
    12.             changechest(newid);
    13.         }
    14.        
    15.     }
    16.  
    17.    [Command]
    18.      void Cmdchangechest(int num)
    19.     {
    20.        //Since this changes, the Hook method is called from server (From my understanding)
    21. echest = num;
    22.     }
    23.  
    24. //Final Action
    25.     public void changechest(int num) {
    26.         if (body.male) {
    27.             if (ChestName == ChestName) {
    28.                 Destroy (ChestInst);
    29.             }
    30.             body.Mesh_male_torso.active = false;
    31.             body.Mesh_male_arms.active = true;
    32.             ChestName = Chest [num].name;
    33.             GameObject NewChest = (GameObject)Instantiate (Resources.Load ("Armor/" + ChestName));
    34.             ChestInst = NewChest;
    35.             Destroy (NewChest);
    36.             AddChest ();
    37.         }
    38.  
    39.     }
    40.  
    I'm still new to this and get real confused a lot of the time with UNET but I'm pushing through and seeing real progress xD