Reputation: 21
I'm having trouble understanding how to use those Rpc's correctly. Sometimes they work sometimes they don't. Example : I have this gameobject that is in the scene (not instantiated on connection), called "Book", it has a script ("SpellManager") so when both player connect, they both have this object in their scene. Now in that SpellManager script, I want my Host/Server to change a spell variable on collision. Let's say I have a "string newSpell" variable, how do I change this variable on the server side and then send the information to the clients ? I have tried something like this (simplified) :
private string newSpell;
private void OnTriggerEnter(Collider other)
{
if (NetworkManager.Singleton.IsServer)
{
newSpell = "Earth";
testClientRpc(Time.frameCount, newSpell);
}
else
{
print("OnClient ");
}
}
[ClientRpc]
public void testClientRpc(int somenumber, string _newSpell)
{
newSpell = _newSpell;
}
But this trows an error on the server : NullReferenceException: Object reference not set to an instance of an object Unity.Netcode.NetworkBehaviour.get_NetworkManager () (at Library/PackageCache/[email protected]/Runtime/Core/NetworkBehaviour.cs:219)
And it's coming from the _newSpell
variable which is apparently null ? I even tried hardcoding newspell = "Earth" in the ClientRpc, same error. An example/video would be much appreciated, I couldn't find anything similar
Upvotes: 2
Views: 5740
Reputation: 53
NetworkBehaviour
(because you are using RPC
) must have a NetworkObject
component attached to it.RPC
from a Non-Owner object, you must set the requireOwnership
option to false in the attribute options i.e. [ServerRpc(requireOwnership = false)]
Upvotes: 2