NullReferenceException when calling RPC

squirrel
squirrel ✭✭
edited January 2022 in Fusion

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

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

  • squirrel
    squirrel ✭✭
    edited January 2022

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

    Other thoughts that might be relevant:

    1. If I completely comment out the contents of the RPC method, the error still gets thrown.
    2. Renaming the RPC method to something like RpcFooBar() doesn't help.
    3. The attribute for the method is just [Rpc]
    4. The RPC method is set to public void
    5. I've tried doing Fusion => Bake Scene Objects and Fusion => Run Weaver
    6. I've tried reimporting both prefabs that contain the two relevant scripts
    7. I've tried the "Reimport All" feature in Unity
  • I just tried switching over to using a [Networked] property to achieve the same functionality, but it throws the same error.

  • squirrel
    squirrel ✭✭
    edited January 2022

    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.

This discussion has been closed.