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.
[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.
Answer by VanToRiaXVII · Feb 23 at 04:44 PM
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.
The RequireOwnership line means that the client can call the function on the server side without being the owner of the object, so it shouldn't change anything.
The really weird part is that the error isn't coming up because the client wants to call the method, it's when the host attempts to call it. If the client tries to call it, a warning saying that the object isn't spawned comes up instead. So it shouldn't be because of any delay.
The thing is, according to the documentation, I shouldn't have to spawn the object, it should be spawned automatically as it is a part of the scene. It specifically says that there is no need to manually spawn them. And they should be spawned in before the Start()
method is called.
Your answer
Follow this Question
Related Questions
Unity 5 Unet Network Rigidbody Syncing - Client sinking through objects. 0 Answers
Networking: How to access local client's owned objects? 0 Answers
How both players can control the soccer ball in 2 player football game 1 Answer
Multiplayer First Person shooter similar to Battlefield 1 Answer
cannot select local player Authority 1 Answer