The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

Try Our

Please check if you can find an answer in our extensive documentation on Fusion.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

NullReferenceException when calling RPC

2022-01-30 23:14:46

Hi there!

I've spent several hours on a strange issue with RPCs. I am getting the following error (RpcDeactivate is the RPC in question):

The line where it is failing is the closing curly brace (}) of the RPC method.

Removing the [Rpc] attribute from the method "fixes" the error.

The error occurs whether I'm playing in SinglePlayer or Shared game mode.

For context from EnemySpawnPicker.cs:96 , I am in fact checking if the script and the GameObject exists before calling RpcDeactivate() :

Additionally, if I Debug.Log() the object, I can see that it does, in fact, exist before RpcDeactivate() is called on it.

I'm at a loss here. Any help would be greatly appreciated.


2022-01-30 23:16:43

I'm using Unity 2021.2.8f1 and Fusion build 0.13.0 RC Nightly 390

2022-01-30 23:32:42

Just tried upgrading to Nightly 400; no luck fixing the problem.

Other thoughts that might be relevant:

  • If I completely comment out the contents of the RPC method, the error still gets thrown.

Renaming the RPC method to something like RpcFooBar()

  • doesn't help.

The attribute for the method is just

  • [Rpc]

The RPC method is set to

  • public void

  • I've tried doing Fusion => Bake Scene Objects and Fusion => Run Weaver

  • I've tried reimporting both prefabs that contain the two relevant scripts

  • I've tried the "Reimport All" feature in Unity

2022-01-31 02:28:02

I just tried switching over to using a [Networked] property to achieve the same functionality, but it throws the same error.

2022-01-31 02:52:08

I finally figured it out!! 🎉🎉🎉🎈🎈🎈🎊🎊🎊💃💃💃

I got thinking more about the error message and realized I hadn't tried Debug.Log(Object) to make sure that the NetworkObject existed. Surprise, surprise: it didn't.

From there I realized that I was executing Fusion code in a game object's Start() method, and essentially it was trying to interact with NetworkObjects too early! This hadn't been a problem in PUN, so I hadn't even considered it.

All I had to do was change my void Start() to a public override void Spawned() and it worked!

Hopefully this can help someone else figure out this headache of a problem :) In the future, Debug.Log(Object) will be one of the first things on my debug checklist.

Back to top