InvalidOperationException: An RPC called on a NetworkObject that is not in the spawned objects list.

I’m getting this exception from a “[ServerRpc(RequireOwnership = false)]” of a NetworkObject (and only sometimes, maybe 1/4 of the time.

I have no clue why this NetworkObject should be in a spawned objects list or what it means, since I’m not spawning anything from the Network Manager.

Any help is appreciate it

Hi @GuirieSanchez , how are you spawning it then? From the docs:

"One of the requirements to be able to spawn a network prefab instance is that it must be registered with the NetworkManager via the “Network Prefabs” property list. The two steps to registering a network prefab with NetworkManager:

  • Create a network prefab by creating a prefab with a NetworkObject component attached to the root GameObject
  • Add your network prefab to the Network Prefabs list property of the NetworkManager.

"

You’ll find the list of NetworkPrefab’s in the NetworkManager inspector. Does this help?

1 Like

Well, it’s just a game object in the scene with a NetworkObject component attached to it (so, I’m not spawning it). I thought it was fine, shouldn’t I do this?

You can do it like this, but you still need to register the prefab. Please have a look at this section of the doc for all the details ^.^

2 Likes

Thank you!

I noticed I had the GO in the spawned objects list, but it wasn’t a prefab.

1 Like

Just right now I’m experiencing this issue all the time. It points to a ClientRpc called from the host at the “OnClientConnected” callback.

When I start the game as a host, it throws this error.

This script (Networkbehaviour) is attached to a prefab that is in the “NetworkManager” script => NetworkPrefabs list.

(1) Could it be that this is not the list the error is talking about?

(2) Or maybe a problem with the Netcode version (1.0.2) (I don’t know how to update it: there’s no update showing at the Package Manager (I’m using Unity 2021.3.12f1))

(3) Or, lastly, may it be a problem with the code?

Try calling the clientRPC from OnNetworkSpawn instead of onClientConnect. The object on the client might not have spawned at that time.

1 Like

thank you for the help. Actually I’m using the clientRpc to send the clientId to the server. I guess the OnNetworkSpawn doesn’t hold that information :frowning:

Do you know of an alternative?

OnClientClientConnectedCallback already sends the client id with it

1 Like

Indeed, it’s exactly what you were talking about. Apparently, OnClientConnected is too early for an Rpc call because the client’s Network is not spawned yet, and it throws exceptions.

I need to send the clientId, the clientGuid, and some other parameters, but that should be fairly straightforward:
On the OnNetworkSpawn callback, I can send a [ServerRpc (RequireOwnership= false)] and then get all the needed parameters.
The client ID could be easily gotten with:

var clientId = serverRpcParams.Receive.SenderClientId;

if I’m not mistaken.

Thank you!

1 Like

ClientRPCs are for servers to send events (or data) to the clients. You’re doing the opposite, so you want to use ServerRPCs.

Which is what you wrote in your last message ^.^

1 Like