Search Unity

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

Discussion in 'Netcode for GameObjects' started by GuirieSanchez, Jan 5, 2023.

  1. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    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
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    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:
    1. Create a network prefab by creating a prefab with a NetworkObject component attached to the root GameObject
    2. 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?
     
    GuirieSanchez likes this.
  3. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    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?
     
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    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 ^.^
     
    Durium and GuirieSanchez like this.
  5. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    Thank you!

    I noticed I had the GO in the spawned objects list, but it wasn't a prefab.
     
    RikuTheFuffs-U likes this.
  6. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    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))
    upload_2023-1-10_18-16-4.png

    (3) Or, lastly, may it be a problem with the code?
     
    Last edited: Jan 10, 2023
  7. Evil-Otaku

    Evil-Otaku

    Joined:
    Oct 17, 2012
    Posts:
    72
    Try calling the clientRPC from OnNetworkSpawn instead of onClientConnect. The object on the client might not have spawned at that time.
     
    GuirieSanchez likes this.
  8. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    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 :(

    Do you know of an alternative?
     
  9. Evil-Otaku

    Evil-Otaku

    Joined:
    Oct 17, 2012
    Posts:
    72
    OnClientClientConnectedCallback already sends the client id with it
     
    GuirieSanchez likes this.
  10. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    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:
    Code (CSharp):
    1. var clientId = serverRpcParams.Receive.SenderClientId;
    if I'm not mistaken.

    Thank you!
     
    Evil-Otaku likes this.
  11. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    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 ^.^
     
    GuirieSanchez likes this.