How to use networkmessages

How I can send a variable through networkmessages?
The unity documentation doesn’t explain it well :confused:

Yeah the docs could give better examples. So the key is for every type of message you want to send you create a network message class that is basically the object you are sending. You create ID’s that you send to indicate the kind of message you’re sending (the “const short MyBeginMsg = 1002;” line in the docs), and you create methods that are called when receiving the message which you register with the server or client.

So what I did was create a separate script I call my networkprotocol, which has a series of class declarations like “public class ScoreMessage : MessageBase” with all the different kinds of message data I would send (such as character info, zone server to master server status messages, etc), and then a series of const short declarations for every kind of message I would send (more than one of these can use the same message class you’ve previously declared).

Then I have a networkclient script and a networkserver script. Those get registrations like “myClient.RegisterHandler(MyBeginMsg, OnCallThisMethodWhenIReceiveBeginMsg);”. I end up with a bunch of On… methods for each message ID registered with this client or server. In the docs look at how “OnScore” is done, that’s a pretty simple example.

https://docs.unity3d.com/Manual/UNetMessages.html