NullReferenceException when calling RPC

The whole answer can be found below.

Please note: 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! And we offer you support through these channels:

Try Our
Documentation

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.

Read More on
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

squirrel
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):

NullReferenceException: Object reference not set to an instance of an object    
EnemySpawner.RpcDeactivate () (at Assets/BL/Scripts/Enemies/EnemySpawner.cs:322)    
EnemySpawnPicker.Start () (at Assets/BL/Scripts/Enemies/EnemySpawnPicker.cs:96)    

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() :

if (    
    enemySpawnerScripts[i] != null    
    && enemySpawnerScripts[i]    
    && enemySpawnerScripts[i].gameObject != null    
    && enemySpawnerScripts[i].gameObject    
) {    
    enemySpawnerScripts[i].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.

Comments

squirrel
2022-01-30 23:16:43

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

squirrel
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

squirrel
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.

squirrel
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