Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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:
    17
    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:
    260
    You're looking for TargetRpc.
     
  3. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    17
    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