Sending Messages with Unet


I’m trying to send a Message over the network, like in this example:

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;

public class Begin : NetworkBehaviour
    const short MyBeginMsg = 1002;

    NetworkClient m_client;

    public void SendReadyToBeginMessage(int myId)
        var msg = new IntegerMessage(myId);
        m_client.Send(MyBeginMsg, msg);

    public void Init(NetworkClient client)
        m_client = client;
        NetworkServer.RegisterHandler(MyBeginMsg, OnServerReadyToBeginMessage);

    void OnServerReadyToBeginMessage(NetworkMessage netMsg)
        var beginMessage = netMsg.ReadMessage<IntegerMessage>();
        Debug.Log("received OnServerReadyToBeginMessage " + beginMessage.value);

the Init method is never called by the system, Its hard to find a working solution with the docs.

Waht I want to test is:

Sending a MSG to the server and the server sould respond to this message with a other msg.

Look at this example, it helped me a lot

Hi sebeisdrache

I know this is an old thread, but for anyone else coming across this
You need to Start your server, which is why the client doesn’t connect in the code above.

(here is my code - using MatchMaking in 5.5 - but the concept is the same).

public virtual void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
		if (success)
			Debug.Log("Match Created");

			MatchInfo hostInfo = matchInfo;

			NetworkServer.Listen(hostInfo, 9000);

            **NetworkServer.RegisterHandler(MsgType.Ready, OnClientVoted);**

            ActionText.text = "created server";

			Debug.LogError("Create match failed");

Important bit that “got me” was that I was registering the Handlers before starting the server (which is stupid of me). Once I put the registration in AFTER the Server started, the events hook up properly.