Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Third Party Send data to specific players/clients with mirror.

Discussion in 'Multiplayer' started by motillaones, Nov 24, 2022.

  1. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    18
    Hello everyone, I am following a mirror room creation guide in which you create instances for each game with an identifier per instance. I find the problem when I send commands within the instances because the server sends it to all the players. Looking at https://mirror-networking.gitbook.io/docs/guides/attributes I know that it's more or less possible but I've been quite confused for a couple of days about the best method to be able to send messages only to players with the identifier of the instance. Does anyone know what is the best way to do it? I was previously using photon and am pretty new with mirror. Thank you!
     
  2. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    414
    You're looking for TargetRpc.
     
  3. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    18
    Hello Punfish!, thanks for answering. I have been researching about the TargetRpc, but I think that for what I need I don't know if they are valid. The message that I want only certain clients to see is the spawn of the player, so I think I am forced to do it through [Command].
    When entering the server, the playerPrefab only has a NetworkIdentity and the script in charge of managing rooms. When entering the first Scene, depending on the game mode, I spawn a type of playerprefab. So I don't know if I'm right that the condition for the second playersPrefab to be visible must be in the [Command] or in a TargetRpc.

    Basically the code is now like this:

    [Command]
    void CmdTest()
    {
    GameObject playerPref = Instantiate(normalPlayer);

    NetworkServer.Spawn(playerPref, connectionToClient);
    TargetDoMagic(playerPref);

    }
    [TargetRpc]
    public void TargetDoMagic(GameObject playerPref)
    {


    copyPlayer = playerPref; //this is to enable some things in the Spawned playerprfab
    }

    on the other hand there is the string MatchID; which differentiates the room in which each player is.
     
    Last edited: Nov 25, 2022