- Home /
Unity messaging problem - cant send from client to server
Hi guys! I really cant handle a problem with sending messages. Every time when I try to send message i get "NullReferenceException: Object reference not set to an instance of an object Message+MasterClient.RegisterHost (System.String name) (at Assets/Message.cs:27) Message.Update () (at Assets/Message.cs:34)". Here is my code, please help if possible
Client: using UnityEngine; using UnityEngine.Networking;
public class Message : NetworkBehaviour
{
class RegisterHostMessage : MessageBase
{
public string gameName;
public string comment;
public bool passwordProtected;
}
class MasterClient
{
public NetworkClient client;
public const short RegisterHotsMsgId = 888;
public void RegisterHost(string name)
{
RegisterHostMessage msg = new RegisterHostMessage();
msg.gameName = name;
msg.comment = "test";
msg.passwordProtected = false;
client.Send(RegisterHotsMsgId, msg);
}
}
void Update()
{
MasterClient mast = new MasterClient ();
mast.RegisterHost ("works");
}
}
Server: using UnityEngine; using UnityEngine.Networking; using System.Collections;
public class ServerStart : NetworkBehaviour {
void Start()
{
Debug.Log ("Registering server callbacks");
}
void Update()
{
NetworkServer.RegisterHandler(888, OnMessage);
}
void OnMessage(NetworkMessage netMsg)
{
Debug.Log ("Client get");
}
}
Answer by cakeslice · Dec 05, 2016 at 09:47 PM
Sorry, there's a lot of problems here, you really should learn the programming basics before you try something like this.
In case you're wondering what's wrong:
Why is this not instantiated (what causes your error):
public NetworkClient client;
Why are you doing this in the Update() instead of Start():
MasterClient mast = new MasterClient ();
Again, why are you doing this every frame:
NetworkServer.RegisterHandler(888, OnMessage);
Cackeslice, thank you for your answer! I am indeed new in coding, but I obviously know that Updade func is a frame func. The problem is that if I paste "NetworkServer.RegisterHandler" in start func literally nothing happens. I just tried an example: using UnityEngine; using UnityEngine.Networking; using System.Collections;
public class ServerStart : NetworkBehaviour {
void Start()
{
Debug.Log ("Registering server callbacks");
NetworkServer.RegisterHandler($$anonymous$$sgType.Connect, OnConnected);
}
void Update()
{
}
void OnConnected(Network$$anonymous$$essage net$$anonymous$$sg)
{
Debug.Log ("Client connected");
}
}
But it works only if I paste "NetworkServer.RegisterHandler" in update func, or else - just nothing, not even an error. Can you help me with this issue?
If it is possible, could you please share an example of code?
Your answer
Follow this Question
Related Questions
How to receive message from NetworkConnection 1 Answer
NullReference Exception FindLocalObject C# 1 Answer
Client with authority can't send information to server 0 Answers
How to watch in client applications what it is happening in the server one? 0 Answers
UNET Different size of spawned object on host and client 1 Answer