Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Frubi · Aug 17, 2016 at 10:00 PM · networkingserverrpcchat

RPC Broadcast using Unet

Hi, i need help with my multiplayer game using Unet, i have a script which creates a chat gui, when i press enter the text gets send to the server using [Command] but how do i send the text to all player instances ? . When i use syncvar or rpc only the original player instance will get the text.

sincerly frubi

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DiegoSLTS · Aug 17, 2016 at 10:15 PM

Can you share your current code of that command and some info on your setup? Your project can be anything.

With that little info I can only tell you that you need a ClientRPC method that your server should call after executing the command. Something like:

 [Client]
 public void SendMessage(string msg) {
     CmdSendMessage(msg);
 }
 
 [Command]
 void CmdSendMessage(string msg){
     chat.AddMessage(msg);
     RpcAddMessage(msg);
 }

 [ClientRpc]
 void RpcAddMessage(string msg) {
     chat.AddMessage(msg);
 }

Also, I noted you said "the player that sent the text"... are you talking about the player (you'll have multiple players for each client, one being the local player) or the client? If you're expecting the SyncVar off all players being updated or the Rpc method of all players being called you're missunderstanding some concepts. The ClientRpc runs on a certain player (the "same" player) in all the clients. Also, make sure you're using ClientRpc and not the new TargetRpc, which actually runs only on one player of the target client.

Comment
Add comment · Show 4 · 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 Frubi · Aug 18, 2016 at 12:53 PM 0
Share

Sure, the concept of the network is: the network manager (Unet) manages all connections (host, client etc.) loads the scene and spawns the player prefab. In this player prefab i added the the networkChat.cs, a other script of the player is checking if the instance is the local player and enables/disables all components. When i use simple RPC only the sender receives his text.

NetworkChat.cs = https://drive.google.com/file/d/0B1IuACgDZHdYX3ZUbnhEQ3NGbGc/view?usp=sharing

avatar image meat5000 ♦ Frubi · Aug 18, 2016 at 12:57 PM 0
Share

"only the sender receives his text".

Seems to me that the actual data is not being transferred across the network.

Are you testing this across multiple devices? Use the editor as the Server and launch a Standalone for the client.

avatar image DiegoSLTS Frubi · Aug 18, 2016 at 01:20 PM 0
Share

Your setup is confusing, each player has it's own chat?

As I said before, when you execute an RPC method in the server for one player, all clients execute that method for that player ONLY. If on each client you're deactivating scripts on players that are not the local player, then there's only one client that will do anything since it's the only cilent with the player with the script with the RPC method enabled. Just to be clear, the RPC method doesn't run for the local player, if you have two players called Player1 and Player2 and you run an RPC method on the server on a script of Player1, all clients will be notified to execute that same method on their copy of Player1, even if it's not that client's local player.

Disabling all components on players that are not the local players is a bad idea, the players must be enabled so the networking makes sense, it's the only way you can synchronize all player objects between all clients.

The basic code to send the text to all clients is what I wrote above, you just execute an RPC for some players and all copies of that player will run that code, the problem here seems to be that you have a "chat" for each player ins$$anonymous$$d of one chat that all players can update, so you must disable the players to show only one chat.

Create one object that handles the chat, make all players update the same chat, and add some reference to the local player. That way you have only one thing that draws the chat and handles input, and when you press Return you can do:

 localPlayer.CmdSendChat(inputField);

ins$$anonymous$$d of:

 CmdSendChat(inputField);

One more detail, the [Client] attribute is for methods that you call yourself. For OnGUI, Update and other $$anonymous$$onoBehaviour methods you must use [ClientCallback].

avatar image Frubi DiegoSLTS · Aug 18, 2016 at 08:33 PM 0
Share

"As I said before, when you execute an RPC method in the server for one player, all clients execute that method for that player ONLY"

I know that, i thought there would be a easy way to execute the rpc on all players.

I will try as you said, thank you, at the moment i dont have much time (and motivation) so it will take a little time untill i answer again

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Network Chat not sending messages to other clients 1 Answer

Sending RPC to add playerName 4 Answers

[UNet] Command not calling RPC on all Clients? 0 Answers

Im not being able to fill up variables with RPCs 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