Search Unity

Resolved Issue with tag attributions

Discussion in 'Netcode for GameObjects' started by Papouth, Feb 12, 2024.

  1. Papouth

    Papouth

    Joined:
    Mar 28, 2020
    Posts:
    8
    Hello, I'm developing a multiplayer game and for some reason I need to assign a tag to each player. Moreover, the players won't have the same tags, each player will receive a tag according to the team he has chosen.

    In addition to the tags assigned to the player, I also assign the necessary scripts to the player for the upcoming game.

    So much for the background.


    As for the problem :

    The script below runs on an object that is not attached to one of my players (i.e. a script that is only present once in my scene).

    Its purpose is to find the players in the police team and assign them their tag and script, and the same for the thieves' team.

    Code (CSharp):
    1. private void Tagging()
    2.     {
    3.         #region Owner
    4.         /*if (IsOwner)
    5.         {
    6.             Debug.Log(TS.copsNamesList[0] + " nom du flic");
    7.  
    8.             // Attribution de la prison à l'un des flics si entre 6 et 8 joueurs
    9.             if (NetworkManager.ConnectedClientsList.Count >= 6)
    10.             {
    11.                 var randomInt = Random.Range(1, 2);
    12.                 Debug.Log("policier choisit : " + randomInt);
    13.  
    14.                 copWithJail = TS.copsNamesList[randomInt];
    15.             }
    16.             else if (NetworkManager.ConnectedClientsList.Count < 6)
    17.             {
    18.                 // Attribution de la prison au seul policier présent dans la partie
    19.                 copWithJail = TS.copsNamesList[0];
    20.             }
    21.  
    22.             foreach (NetworkClient item in NetworkManager.ConnectedClientsList)
    23.             {
    24.                 PI = item.PlayerObject.gameObject.GetComponent<PlayerInfo>();
    25.  
    26.                 if (TS.copsNamesList.Contains(PI.playerName))
    27.                 {
    28.                     //Debug.Log(item.PlayerObject.name + " est un policier");
    29.  
    30.                     PI.isCops = true;
    31.                     PI.isCopsInt = 1;
    32.  
    33.                     item.PlayerObject.gameObject.tag = "cops";
    34.  
    35.                     PI.UpdateServerInfoClientRpc(true, 1);
    36.  
    37.                     item.PlayerObject.gameObject.GetComponent<WeaponCop>().enabled = true;
    38.  
    39.                     // S'il s'agit du policier ayant la prison
    40.                     if (PI.playerName == copWithJail)
    41.                     {
    42.                         // On enable le script de la prison sur le joueur
    43.                         item.PlayerObject.gameObject.GetComponent<VirtualJail>().enabled = true;
    44.  
    45.                         PI.UpdateServerRoleJailClientRpc(true);
    46.                     }
    47.                     else if (PI.playerName != copWithJail)
    48.                     {
    49.                         Destroy(item.PlayerObject.gameObject.GetComponent<VirtualJail>());
    50.  
    51.                         PI.UpdateServerRoleJailClientRpc(false);
    52.                     }
    53.                 }
    54.                 else if (TS.runnersNamesList.Contains(PI.playerName))
    55.                 {
    56.                     //Debug.Log(item.PlayerObject.name + " est un voleur");
    57.  
    58.                     PI.isCops = false;
    59.                     PI.isCopsInt = 2;
    60.  
    61.                     item.PlayerObject.gameObject.tag = "runners";
    62.  
    63.                     PI.UpdateServerInfoClientRpc(false, 2);
    64.                     Destroy(item.PlayerObject.gameObject.GetComponent<VirtualJail>());
    65.  
    66.                     Destroy(item.PlayerObject.gameObject.GetComponent<WeaponCop>());
    67.                     Destroy(item.PlayerObject.gameObject.GetComponentInChildren<CapturePlayer>().gameObject);
    68.                     PI.UpdateServerRoleJailClientRpc(false);
    69.                     PI.UpdateServerRoleCaptureClientRpc(false);
    70.                 }
    71.             }
    72.         }*/
    73.         #endregion
    74.  
    75.  
    76.         ////
    77.  
    78.  
    79.         #region Server
    80.         if (IsOwner)
    81.         {
    82.             Debug.Log(TS.copsNamesList[0] + " nom du flic");
    83.  
    84.             // Attribution de la prison à l'un des flics si entre 6 et 8 joueurs
    85.             if (NetworkManager.ConnectedClientsList.Count >= 6)
    86.             {
    87.                 var randomInt = Random.Range(1, 2);
    88.                 Debug.Log("policier choisit : " + randomInt);
    89.  
    90.                 copWithJail = TS.copsNamesList[randomInt];
    91.             }
    92.             else if (NetworkManager.ConnectedClientsList.Count < 6)
    93.             {
    94.                 // Attribution de la prison au seul policier présent dans la partie
    95.                 copWithJail = TS.copsNamesList[0];
    96.             }
    97.  
    98.             foreach (NetworkClient item in NetworkManager.ConnectedClientsList)
    99.             {
    100.                 PI = item.PlayerObject.gameObject.GetComponent<PlayerInfo>();
    101.  
    102.                 ClientRpcParams clientRpcParams = new ClientRpcParams
    103.                 {
    104.                     Send = new ClientRpcSendParams
    105.                     {
    106.                         TargetClientIds = new ulong[] { item.ClientId }
    107.                     }
    108.                 };
    109.  
    110.                 if (TS.copsNamesList.Contains(PI.playerName))
    111.                 {
    112.                     //Debug.Log(item.PlayerObject.name + " est un policier");
    113.  
    114.                     PI.isCops = true;
    115.                     PI.isCopsInt = 1;
    116.  
    117.                     item.PlayerObject.gameObject.tag = "cops";
    118.  
    119.                     PI.UpdateServerInfoClientRpc(true, 1, clientRpcParams);
    120.  
    121.                     item.PlayerObject.gameObject.GetComponent<WeaponCop>().enabled = true;
    122.  
    123.                     // S'il s'agit du policier ayant la prison
    124.                     if (PI.playerName == copWithJail)
    125.                     {
    126.                         // On enable le script de la prison sur le joueur
    127.                         item.PlayerObject.gameObject.GetComponent<VirtualJail>().enabled = true;
    128.  
    129.                         PI.UpdateServerRoleJailClientRpc(true, clientRpcParams);
    130.                     }
    131.                     else if (PI.playerName != copWithJail)
    132.                     {
    133.                         Destroy(item.PlayerObject.gameObject.GetComponent<VirtualJail>());
    134.  
    135.                         PI.UpdateServerRoleJailClientRpc(false, clientRpcParams);
    136.                     }
    137.                 }
    138.                 else if (TS.runnersNamesList.Contains(PI.playerName))
    139.                 {
    140.                     //Debug.Log(item.PlayerObject.name + " est un voleur");
    141.  
    142.                     PI.isCops = false;
    143.                     PI.isCopsInt = 2;
    144.  
    145.                     item.PlayerObject.gameObject.tag = "runners";
    146.  
    147.                     PI.UpdateServerInfoClientRpc(false, 2, clientRpcParams);
    148.                     Destroy(item.PlayerObject.gameObject.GetComponent<VirtualJail>());
    149.  
    150.                     Destroy(item.PlayerObject.gameObject.GetComponent<WeaponCop>());
    151.                     Destroy(item.PlayerObject.gameObject.GetComponentInChildren<CapturePlayer>().gameObject);
    152.                     PI.UpdateServerRoleJailClientRpc(false, clientRpcParams);
    153.                     PI.UpdateServerRoleCaptureClientRpc(false, clientRpcParams);
    154.                 }
    155.             }
    156.         }
    157.         #endregion


    Then, as far as ClientRpc are concerned, they are present on one of my player's scripts (so each player has this script on him).

    With the help of the Unity documentation, I try to send the ClientRpc request only to the player concerned.



    Code (CSharp):
    1. [ClientRpc]
    2.     public void UpdateServerInfoClientRpc(bool playerIsCops, int playerIsCopsInt, ClientRpcParams clientRpcParams = default)
    3.     {
    4.         if (IsOwner) return;
    5.  
    6.         isCopsInt = playerIsCopsInt;
    7.         isCops = playerIsCops;
    8.  
    9.         Debug.Log("Passe serverInfoClientRpc");
    10.  
    11.         if (playerIsCops)
    12.         {
    13.             gameObject.tag = "cops";
    14.             Debug.Log("playerInfo tag Cops");
    15.         }
    16.         else if (!playerIsCops)
    17.         {
    18.             gameObject.tag = "runners";
    19.             Debug.Log("playerInfo tag Runners");
    20.         }
    21.     }
    22.  
    23.     [ClientRpc]
    24.     public void UpdateServerRoleJailClientRpc(bool playerJail, ClientRpcParams clientRpcParams = default)
    25.     {
    26.         if (IsOwner) return;
    27.  
    28.         haveJail = playerJail;
    29.  
    30.         if (!haveJail && VJ != null) Destroy(VJ);
    31.         else if (haveJail && !VJ.enabled) VJ.enabled = true;
    32.     }
    33.  
    34.     [ClientRpc]
    35.     public void UpdateServerRoleCaptureClientRpc(bool playerCoporNot, ClientRpcParams clientRpcParams = default)
    36.     {
    37.         if (IsOwner) return;
    38.  
    39.         playerCop = playerCoporNot;
    40.  
    41.         if (!playerCoporNot && WC != null) Destroy(WC);
    42.         else if (playerCoporNot && !WC.enabled) WC.enabled = true;
    43.  
    44.         if (!playerCoporNot && captureCol != null) Destroy(captureCol);
    45.     }


    So :Using the Unity NGO documentation and checking whether it's the server or the owner, nothing happens.

    On the other hand, if I don't follow the Unity documentation, and don't try to send only the Rpc call to the targeted client, then I can expect it to work for the host only.


    My question is this:

    Did I make a mistake in my code by following the documentation provided by Unity, which would explain why it doesn't work ?

    Or does the problem come from somewhere else in my code?

    If you need more information to help me solve my problem I'll provide it.

    I've been stalling and trying different solutions for over 3 weeks now, but I'm really starting to despair!
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,789
    You dont appear to be sending the clientid of a player so you send to all players - was this intentional?
     
  3. Papouth

    Papouth

    Joined:
    Mar 28, 2020
    Posts:
    8
    In the first part of script, l 98 to 108 I set a new ClientRpcParams with the id of the player.
    Also, I do this for each player of my list.

    And then I sent it with the clientRpcParams in each client rpc call.
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,789
    I missed it due to the huge comment.. However, surely you pick your tag on the client, send it to the server with a serverpc,l and then update the client tag there, or a network variable?
     
  5. Papouth

    Papouth

    Joined:
    Mar 28, 2020
    Posts:
    8
    I can't pick my tag directly on my player, since I use a team selecting menu, who just register the player name (and not the player object itself).


    EDIT: Well I maybe talk to fast. I will try yo move my actual script on my player and try what you suggest.

    I come back later in the day to update my progression on this issue.

    Thank a lot, hope this will work ;)
     
  6. Papouth

    Papouth

    Joined:
    Mar 28, 2020
    Posts:
    8
    Well thank a lot @bugfinders it's seem to work pretty good. :D
     
    bugfinders likes this.
  7. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,789