MLAPI NetworkObject not spawned in as part of the scene,MLAPI NetworkObject not spawned when part of scene

I am a beginner with MLAPI so I could have missed something, but from what I understand this should work and I would like to know why it doesn’t work even though I have found a rather hacky solution. So I have a NetworkObject with a NetworkBehaviour script that runs a simple test of logging the client id at Start() by calling a ServerRpc, but for some reason it’s giving an error message.

[Netcode] Tried to invoke an RPC on a non-existent NetworkObject with canDefer=false

NetworkBehaviour script:

public class Tester : NetworkBehaviour {
    private void Start() {
        TestServerRpc(NetworkManager.Singleton.LocalClientId);
    }

    [ServerRpc(RequireOwnership = false)]
    public void TestServerRpc(ulong clientId) {
        Debug.Log($"Client id {clientId}");
    }
}

It does work if I spawn it manually from the inspector or change the Start() method to:

private void Start() {
    if (IsServer || IsHost)
        GetComponent<NetworkObject>().Spawn();
    
    TestServerRpc(NetworkManager.Singleton.LocalClientId);
}

So I assume that the NetworkObject isn’t spawned in when part of the scene, but according to the documentation they should automatically be spawned.

1: https://docs-multiplayer.unity3d.com/docs/basics/object-spawning/index.html#scene-objects,I am a beginner with MLAPI so I could have missed something, but from what I understand this should work and I would like to know why it acts differently even though I have found a rather hacky solution. So I have a NetworkObject with a NetworkBehaviour script that runs a simple test of logging the client id at Start() by calling a ServerRpc, but for some reason it’s giving an error message.

[Netcode] Tried to invoke an RPC on a non-existent NetworkObject with canDefer=false

NetworkBehaviour script:

public class Tester : NetworkBehaviour {
    private void Start() {
        TestServerRpc(NetworkManager.Singleton.LocalClientId);
    }

    [ServerRpc(RequireOwnership = false)]
    public void TestServerRpc(ulong clientId) {
        Debug.Log($"Client id {clientId}");
    }
}

It does work if I spawn it manually from the inspector or change the Start() method to:

private void Start() {
    if (IsServer || IsHost)
        GetComponent<NetworkObject>().Spawn();
    
    TestServerRpc(NetworkManager.Singleton.LocalClientId);
}

So I assume that the NetworkObject isn’t spawned in when part of the scene, but according to the documentation they should automatically be spawned.

The server should have authority over the network object, from what I could read over the documentation. So, when you spawn it in the start method, it works. However, your previous code has the RequireOwnership set to false, so maybe that’s the problem and the NetworkObject might not replicate on the client because of that.

Another idea to help you fix it is that you might have a delay in your code between the moment the object is automatically spawned on the server and the moment it’s available on the client. So, the error could stem from there and have nothing to do with the RequireOwnership line.