- Home /
[ServerRpc] Will return an error: "NullReferenceException: Object reference not set to an instance of an object"
NullReferenceException: Object reference not set to an instance of an object Unity.Netcode.NetworkBehaviour.get_NetworkManager () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.4/Runtime/Core/NetworkBehaviour.cs:219) rpc.MessageServerRpc () (at Assets/rpc.cs:40) rpc.Update () (at Assets/rpc.cs:21)
here is my code:
[SerializeField] float Countdown = 3f;
//Update is called once per frame
void Update()
{
Countdown -= Time.deltaTime;
if (Countdown <= 0)
{
if (!NetworkManager.Singleton.IsHost)
{
Debug.Log("Lets tell the server to message");
MessageServerRpc();
}
else
MessageClientRpc();
Countdown = 4f;
}
}
[ServerRpc(RequireOwnership = false)] //also tried without the requireownership
private void MessageServerRpc()
{
Debug.Log("Ive Got This Running In My Server");
}
i think i just dont know how to run Rpc calls. for some reason, both the server and the clients are calling this method and executing. (when only the server should execute)
why is this happening?
Answer by yonatanab1 · Jan 14 at 12:38 PM
Solved: in order to send Rpc requests you must make sure they are attached to an object that has "NetworkObject" component and that object is added to the "NetworkObjects" list on the "NetworkManager" Component/object you have.
i tried adding a script to the NetworkManager and it did not work.
my suggestion is to create a new "OnlineManager" script (or whatever name you want) and add a "NetworkObject" component to it, so you can place all your network scripts there and easly access them later. also don't forget to add that object to the network objects list at the NetworkManager.
Your answer

Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Force local client RPC to fire immediately? 0 Answers
Network RCP between client server projects 0 Answers
Is server the sender of RPC? 0 Answers