• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Ericky14 · Jun 13, 2016 at 06:09 AM · multiplayer-networkingmessageunity 5.2

Unity Multiplayer Send Message to Specific Client

Hello. I am trying to make a multiplayer card game. For that, I believe I need to send messages to the individual clients whenever there's an action. Such as when the game starts, each player will have their board with different orientations because both of them have their "base" on the bottom.

I already tried registering a handler and using the connectionToClient.Send() method, but it does not work for some reason.

     class SpawnMsg : MessageBase { }
     public override void OnServerAddPlayer(NetworkConnection conn, short id) {
         base.OnServerAddPlayer (conn, id);
  
         playerNumber++;
         conn.RegisterHandler (MsgSpawn, gameManager.SpawnObjectMessage);
         conn.Send (MsgSpawn, new SpawnMsg());
     }
 
 
 //different class
    public void SpawnObjectMessage(NetworkMessage msg) {
         Debug.Log ("MESSAGE RECEIVED");
     }



Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SystemIdle · Oct 30, 2017 at 12:41 AM 0
Share

Did you find a solution to message specific clients?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ericky14 · Oct 31, 2017 at 02:12 AM

I figured out a solution, though I am not sure it is the best method.

     public override void OnStartLocalPlayer() {
         base.OnStartLocalPlayer();    
         CmdReady(CustomNetworkManager.localPlayerUsername);
     }

     [Command]
     void CmdReady(string username) {    
         if (CustomNetworkManager.player1 == null) {
             CustomNetworkManager.player1 = this;
             this.username = username;
         } else if (CustomNetworkManager.player2 == null) {
             CustomNetworkManager.player2 = this;
             this.username = username;
             RpcReady(username);
         }
     }

This way, when RpcReady is called, both users already connected and the server knows their username. Also, username is a syncvar.

 [SyncVar(hook = "OnUsernameChange")]
     public string username;


And when RpcReady is called, it will be called on the second player, so you can follow the same pattern to notify the client and server of each different player.

Well, sorry if it's hard for me to explain. But the main thing is, first understand how these Commands and Rpc calls work, then just juggle them.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SystemIdle · Oct 31, 2017 at 04:29 AM 0
Share

Cool thanks. Yeah I did manage to figure it out in the end. Understanding how [Command] and [TargetRPG] work was definitely the challenging part! The docs really don't explain very well how these work: https://docs.unity3d.com/ScriptReference/Networking.TargetRpcAttribute.html

So basically from my custom Network$$anonymous$$anager I store the NetworkConnection of each client in a list when OnServerConnect is triggered, and call the [TargetRPG] method on a NetworkBehaviour class passing in the NetworkConnection of the client I want to target. The confusing bit was I didn't know I needed to call the [TargetRPG] method and not the [Command] method and how the [Command] method actually works. It is somehow called from/after the [TargetRPG] method is called and it uses NetworkBehaviour.connectionToClient to workout which is the target client, and then re-envokes the [TargetRPG] method on target Client. Not too confusing then ;)

Here's an outline of my code in case it's helpful to someone

 public class CustomNetwork$$anonymous$$anager : Network$$anonymous$$anager
 {
     public override void OnServerConnect(NetworkConnection conn)
     {
         //Store conn in a List
     }
 }
 
 
 public class Example : NetworkBehaviour
 {
     [TargetRpc] // Call this method from Server passing in selected NetworkConnection from List
     public void TargetDo$$anonymous$$agic(NetworkConnection target, string imgURL)
     {
         debugText.text = "$$anonymous$$agic = " + target +"  "+ imgURL;
     }
 
     [Command] // This is somehow called when TargetDo$$anonymous$$agic() is called, works out which Client to target and then envokes TargetDo$$anonymous$$agic() again on that Client?
     void CmdTest()
     {
         TargetDo$$anonymous$$agic(connectionToClient, "");
     }
 }






Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Send Custom Network Message Unity Multiplayer 0 Answers

How do I reconcile discrepancies between client side prediction and my authoritative server? 1 Answer

I cant't see the texture of the other player in my multiplayer game. 1 Answer

Having problems with multiplayer syncing (repository) 0 Answers

How to Sync IK using UNET ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges