Issue with tag attributions

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.

private void Tagging()
    {
        #region Owner
        /*if (IsOwner)
        {
            Debug.Log(TS.copsNamesList[0] + " nom du flic");

            // Attribution de la prison à l'un des flics si entre 6 et 8 joueurs
            if (NetworkManager.ConnectedClientsList.Count >= 6)
            {
                var randomInt = Random.Range(1, 2);
                Debug.Log("policier choisit : " + randomInt);

                copWithJail = TS.copsNamesList[randomInt];
            }
            else if (NetworkManager.ConnectedClientsList.Count < 6)
            {
                // Attribution de la prison au seul policier présent dans la partie
                copWithJail = TS.copsNamesList[0];
            }

            foreach (NetworkClient item in NetworkManager.ConnectedClientsList)
            {
                PI = item.PlayerObject.gameObject.GetComponent<PlayerInfo>();

                if (TS.copsNamesList.Contains(PI.playerName))
                {
                    //Debug.Log(item.PlayerObject.name + " est un policier");

                    PI.isCops = true;
                    PI.isCopsInt = 1;

                    item.PlayerObject.gameObject.tag = "cops";

                    PI.UpdateServerInfoClientRpc(true, 1);

                    item.PlayerObject.gameObject.GetComponent<WeaponCop>().enabled = true;

                    // S'il s'agit du policier ayant la prison
                    if (PI.playerName == copWithJail)
                    {
                        // On enable le script de la prison sur le joueur
                        item.PlayerObject.gameObject.GetComponent<VirtualJail>().enabled = true;

                        PI.UpdateServerRoleJailClientRpc(true);
                    }
                    else if (PI.playerName != copWithJail)
                    {
                        Destroy(item.PlayerObject.gameObject.GetComponent<VirtualJail>());

                        PI.UpdateServerRoleJailClientRpc(false);
                    }
                }
                else if (TS.runnersNamesList.Contains(PI.playerName))
                {
                    //Debug.Log(item.PlayerObject.name + " est un voleur");

                    PI.isCops = false;
                    PI.isCopsInt = 2;

                    item.PlayerObject.gameObject.tag = "runners";

                    PI.UpdateServerInfoClientRpc(false, 2);
                    Destroy(item.PlayerObject.gameObject.GetComponent<VirtualJail>());

                    Destroy(item.PlayerObject.gameObject.GetComponent<WeaponCop>());
                    Destroy(item.PlayerObject.gameObject.GetComponentInChildren<CapturePlayer>().gameObject);
                    PI.UpdateServerRoleJailClientRpc(false);
                    PI.UpdateServerRoleCaptureClientRpc(false);
                }
            }
        }*/
        #endregion


        ////


        #region Server
        if (IsOwner)
        {
            Debug.Log(TS.copsNamesList[0] + " nom du flic");

            // Attribution de la prison à l'un des flics si entre 6 et 8 joueurs
            if (NetworkManager.ConnectedClientsList.Count >= 6)
            {
                var randomInt = Random.Range(1, 2);
                Debug.Log("policier choisit : " + randomInt);

                copWithJail = TS.copsNamesList[randomInt];
            }
            else if (NetworkManager.ConnectedClientsList.Count < 6)
            {
                // Attribution de la prison au seul policier présent dans la partie
                copWithJail = TS.copsNamesList[0];
            }

            foreach (NetworkClient item in NetworkManager.ConnectedClientsList)
            {
                PI = item.PlayerObject.gameObject.GetComponent<PlayerInfo>();

                ClientRpcParams clientRpcParams = new ClientRpcParams
                {
                    Send = new ClientRpcSendParams
                    {
                        TargetClientIds = new ulong[] { item.ClientId }
                    }
                };

                if (TS.copsNamesList.Contains(PI.playerName))
                {
                    //Debug.Log(item.PlayerObject.name + " est un policier");

                    PI.isCops = true;
                    PI.isCopsInt = 1;

                    item.PlayerObject.gameObject.tag = "cops";

                    PI.UpdateServerInfoClientRpc(true, 1, clientRpcParams);

                    item.PlayerObject.gameObject.GetComponent<WeaponCop>().enabled = true;

                    // S'il s'agit du policier ayant la prison
                    if (PI.playerName == copWithJail)
                    {
                        // On enable le script de la prison sur le joueur
                        item.PlayerObject.gameObject.GetComponent<VirtualJail>().enabled = true;

                        PI.UpdateServerRoleJailClientRpc(true, clientRpcParams);
                    }
                    else if (PI.playerName != copWithJail)
                    {
                        Destroy(item.PlayerObject.gameObject.GetComponent<VirtualJail>());

                        PI.UpdateServerRoleJailClientRpc(false, clientRpcParams);
                    }
                }
                else if (TS.runnersNamesList.Contains(PI.playerName))
                {
                    //Debug.Log(item.PlayerObject.name + " est un voleur");

                    PI.isCops = false;
                    PI.isCopsInt = 2;

                    item.PlayerObject.gameObject.tag = "runners";

                    PI.UpdateServerInfoClientRpc(false, 2, clientRpcParams);
                    Destroy(item.PlayerObject.gameObject.GetComponent<VirtualJail>());

                    Destroy(item.PlayerObject.gameObject.GetComponent<WeaponCop>());
                    Destroy(item.PlayerObject.gameObject.GetComponentInChildren<CapturePlayer>().gameObject);
                    PI.UpdateServerRoleJailClientRpc(false, clientRpcParams);
                    PI.UpdateServerRoleCaptureClientRpc(false, clientRpcParams);
                }
            }
        }
        #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.

[ClientRpc]
    public void UpdateServerInfoClientRpc(bool playerIsCops, int playerIsCopsInt, ClientRpcParams clientRpcParams = default)
    {
        if (IsOwner) return;

        isCopsInt = playerIsCopsInt;
        isCops = playerIsCops;

        Debug.Log("Passe serverInfoClientRpc");

        if (playerIsCops)
        {
            gameObject.tag = "cops";
            Debug.Log("playerInfo tag Cops");
        }
        else if (!playerIsCops)
        {
            gameObject.tag = "runners";
            Debug.Log("playerInfo tag Runners");
        }
    }

    [ClientRpc]
    public void UpdateServerRoleJailClientRpc(bool playerJail, ClientRpcParams clientRpcParams = default)
    {
        if (IsOwner) return;

        haveJail = playerJail;

        if (!haveJail && VJ != null) Destroy(VJ);
        else if (haveJail && !VJ.enabled) VJ.enabled = true;
    }

    [ClientRpc]
    public void UpdateServerRoleCaptureClientRpc(bool playerCoporNot, ClientRpcParams clientRpcParams = default)
    {
        if (IsOwner) return;

        playerCop = playerCoporNot;

        if (!playerCoporNot && WC != null) Destroy(WC);
        else if (playerCoporNot && !WC.enabled) WC.enabled = true;

        if (!playerCoporNot && captureCol != null) Destroy(captureCol);
    }

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!

You dont appear to be sending the clientid of a player so you send to all players - was this intentional?

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.

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?

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 :wink:

Well thank a lot @bugfinders it’s seem to work pretty good. :smile:

1 Like

yay