question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tried to invoke an RPC on a non-existent NetworkObject with canDefer=false from `OnNetworkSpawn()`

See original GitHub issue

Describe 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:

  1. Download TestingSandbox.zip with a small test case of what I’m talking about.
  2. Open up SampleScene
  3. Click on the Play button to go into play mode
  4. 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:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ShadauxCatcommented, Dec 9, 2021

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’s NetworkObjectId 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 each OnNetworkSpawn 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.

1reaction
will-mearnscommented, Nov 10, 2021

Added to our backlog as MTT-1705

Read more comments on GitHub >

github_iconTop Results From Across the Web

An RPC called on a NetworkObject that is not in ...
Try calling the clientRPC from OnNetworkSpawn instead of onClientConnect. The object on the client might not have spawned at that time.
Read more >
RPC fails if NetworkObject is not initially active · Issue #262
When I attempt the RPC, the objects 'are' active, yet it fails. So you are searching at a time that they are not...
Read more >
NetworkBehaviour
NetworkBehaviour s can use NetworkVariable s and RPC s to synchronize ... OnNetworkSpawn is invoked relative to the NetworkObject type: ...
Read more >
Netcode For Gameobjects : ServerRPC not calling on client
The ServerRPC needs to call a ClientRPC so the clients will update. I haven't read your code, because there's a lot of it...
Read more >
ServerRpc
A ServerRpc is a remote procedure call (RPC) that can be only invoked ... a client owner (client that owns NetworkObject associated with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found