Tried to invoke an RPC on a non-existent NetworkObject with canDefer=false from `OnNetworkSpawn()`
See original GitHub issueDescribe the bug
When calling a ServerRpc from OnNetworkSpawn()
on an instantiated NetworkObject
it causes an error [Netcode] Tried to invoke an RPC on a non-existent NetworkObject with canDefer=false
. I’m assuming because the attached NetworkObject
isn’t quite set up on that frame as waiting for the next frame it will work fine.
The crux of it in this block of code
public override void OnNetworkSpawn()
{
DoThing(); // this doesn't work and caused error (bug?)
StartCoroutine(DoThingAfterFrame()); // this does work
}
private IEnumerator DoThingAfterFrame()
{
yield return new WaitForEndOfFrame();
DoThing();
}
private void DoThing()
{
FindObjectOfType<NetworkTest>().TestServerRpc("This works");
}
To Reproduce Steps to reproduce the behaviour:
- Download TestingSandbox.zip with a small test case of what I’m talking about.
- Open up
SampleScene
- Click on the Play button to go into play mode
- You will see the error in the console.
Actual outcome
Error in the console saying [Netcode] Tried to invoke an RPC on a non-existent NetworkObject with canDefer=false
Expected outcome
For NetworkObject
to be already set up on this frame. OnNetworkSpawn()
would imply to me that everything is set up and ready to use.
Environment (please complete the following information):
- OS: Windows 10
- Unity Version: 2021.2.0f1
- Netcode Version: 1.0.0-pre.2
Additional context I’ve also mentioned the issue on Discord but there isn’t any more information than what is here
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
I took another look at this and found that not only did #1444 not really fix it, this is also something we don’t think we can really fix. Like you mentioned, the
NetworkTest
object hasn’t spawned yet… even trying to defer and queue things, we can’t process an RPC because the object’sNetworkObjectId
hasn’t been assigned yet.I’ll update with a better error message, but there’s no magic we see that we can do here that won’t cause other, likely worse issues. But the good news is that this issue only happens in
OnNetworkSpawn
- what’s happening in this case is that both objects were placed in the scene in the editor and the spawn order for objects in the scene when the server starts is undefined, and eachOnNetworkSpawn
is called as each object spawns. So if you can manually control the spawn order in some way, you can do things like this, but if you’re relying on the SDK to spawn things automatically for you, that’s when you’ll run into this. Don’t know if that’ll help at all, but hopefully it helps to explain the situation.I’m going to go ahead and close this issue, but feel free to comment if you have further questions.
Added to our backlog as MTT-1705