I’m making a multiplayer game using Lidgren and I am fairly new to unity. I have a couple of scripts that I need to communicate with one another through scenes, one of which is a server which can’t be repeatedly made as I need the server to stay constant throughout the scenes. In script A, the server, there is a method to send a message to all clients that requires some parameters and I added this in order to allow other scripts to access the method.
public static NetworkServer Instance { get; private set; }
public static void SendMessage(string text, NetDeliveryMethod deliveryMethod)
{
NetOutgoingMessage sendMsg = Instance.server.CreateMessage();
sendMsg.Write(text);
Instance.server.SendToAll(sendMsg, deliveryMethod);
}
The second script, in a different scene calls it like this:
public void SendColourMatchMessage()
{
NetworkServer.SendMessage("CMStart", NetDeliveryMethod.ReliableOrdered);
}
The communication between scripts appears to work through debugging, but the line:
NetOutgoingMessage sendMsg = Instance.server.CreateMessage();
throws a NullReferenceException:
NullReferenceException: Object reference not set to an instance of an object
NetworkServer.SendMessage (System.String text, Lidgren.Network.NetDeliveryMethod deliveryMethod) (at Assets/NetworkServer.cs:82)
I’m assuming this means there is no instance of the script, but I don’t exactly understand this and would appreciate any help.