Getting an InvalidOperationException

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.

Getting an "InvalidOperationException: Behaviour not initialized: Object not set." with RPCs

Flaemnova
2022-06-17 14:14:07

Hello,

I am trying to sync a game launch event for a multiplayer game from a game session.

The following is my instance RPC, the networked variable and place I call it from:

    [Networked(OnChanged = nameof(OnStateChanged))]     
    public NetworkBool LaunchGame { get; set; }    

    ...    

    // if player count for a session is full, launch game    
    Debug.Log("Player count reached."); // this line is printed out in the console    
    RPC_LaunchGame(true); // any other Log statements aren't printed past this line    
                    
    ...    

    [Rpc(sources: RpcSources.All, targets: RpcTargets.All)]    
    public void RPC_LaunchGame(NetworkBool state) LaunchGame = state;     

    ...    

    private static void OnStateChanged(Changed<GameLauncher> changed)    
    {    
        if(changed.Behaviour) changed.Behaviour.startGame();    
    }    

I get the following Exception:

InvalidOperationException: Behaviour not initialized: Object not set.    
Fusion.NetworkBehaviourUtils.ThrowIfBehaviourNotInitialized (Fusion.NetworkBehaviour behaviour) (at <147a77adcf864b659935f862fe8f5e7b>:0)    

I'm not sure if i'm missing any necessary steps for using RPCs.

From using Log statements, I'm certain the the exception is thrown when the program reaches :

RPC_LaunchGame(true);    

Any advice for tackling RPCs would be appreciated

Comments

Isaac_Augusto
2022-06-17 18:19:57

Hi,

LaunchGame is a networked variable, so it need to be correctly initialized before being used.

If I understood correctly, you're trying to change it before even starting the runner, supposing that startGame() starts the NetworkRunner.

Make sure to only use networked variables after the Runner is launched and the NetworkBehaviour was correctly linked in the simulation.


Isaac Augusto

Photon Fusion Team

Flaemnova
2022-06-18 13:02:32

I do create a gameobject and attach a networkRunner component on Start( ) (script is attached in the scene)

void Awake

()    
    {    
        GameObject go = new GameObject("Session");    
        DontDestroyOnLoad(go);    


        _runner = go.AddComponent<NetworkRunner>();    
        _runner.AddCallbacks(this);    
    }    

Do I need to pass the variable to the runner to initialize it?

What do you mean by linking the NetworkBehaviour to the Simulation?

Thank you for your reply 😀

Isaac_Augusto
2022-06-20 09:56:01

Hi,

As i said, networked variables can be used only when they are correctly initialised. For that, the Runner needs to be started already (with NetworkRunner.StartGame() ).

For what i see, the variable LaunchGame is used before the NetworkRunner have been started, on the RPC_LaunchGame, and that's causing the error.


Isaac Augusto

Photon Fusion Team

knipsch
2022-07-01 20:03:48

In my case, I got this error because the NetworkBehaviour I was trying to call the RPC from, which was on a gameObject that had been placed in the scene at edit time and was not spawned at runtime, wasn't baked correctly. Playing the scene baked it, and I stopped getting the error after I did that. (Previously, I hadn't hit play on that scene, only entered the scene from a different scene while playing).

Ajit
2023-02-06 06:23:06

Hi @knipsch .. I am getting the same error under same circumstances.. where I am coming from the HUB scene to the selected game scene. Here, On GameManager which is a NetworkBehavior already in the scene, when i call a RPC function, getting the issue of "Behavior not initialized: Object not set".

I did rebuild the table for NetworkConfig and also played the scene once. Still i am getting the same error. Please let me know of any other steps that i am missing here

Syncoplay
2023-02-21 21:36:19

Ajit 2023-02-06T06:23:06+00:00

Hi @knipsch .. I am getting the same error under same circumstances.. where I am coming from the HUB scene to the selected game scene. Here, On GameManager which is a NetworkBehavior already in the scene, when i call a RPC function, getting the issue of "Behavior not initialized: Object not set".

I did rebuild the table for NetworkConfig and also played the scene once. Still i am getting the same error. Please let me know of any other steps that i am missing here

@Ajit - It's probably too late, as it's been a few weeks, but I just ran into this same problem and it was because the NetworkObject I was trying to call the RPC on was a child of the NetworkRunner object. There can't be a NetworkObject anywhere in the hierarchy under the NetworkRunner or things get wonky. Hope this helps :)

RemyGamapi
2023-03-08 17:25:06

Ajit 2023-02-06T06:23:06+00:00

Hi @knipsch .. I am getting the same error under same circumstances.. where I am coming from the HUB scene to the selected game scene. Here, On GameManager which is a NetworkBehavior already in the scene, when i call a RPC function, getting the issue of "Behavior not initialized: Object not set".

I did rebuild the table for NetworkConfig and also played the scene once. Still i am getting the same error. Please let me know of any other steps that i am missing here

Hi, I had the same problem and solved it by loading the faulty scene with the fusion scene manager instead of the Unity one.

Back to top