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

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

  • 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
    Flaemnova
    edited June 2022

    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 😀

  • 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
    knipsch
    edited July 2022

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

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

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