OnRoomPropertiesUpdate seems to be called on destroyed objects as well

Options
I have the following situation. I have a SceneOverlord object in my main game level which implements MonoBehaviourPunCallbacks and makes use of OnRoomPropertiesUpdate callback and it works fine, but I recently implemented a game loop where I go back to the lobby level and then start the game level again. Now I've made sure this object does not use DontDestroyOnLoad so it's not persistent, but a strange error appears when I load the main level for the second time, the console throws a
MissingReferenceException: The object of type 'SceneOverlord' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

And this is thrown inside OnRoomPropertiesUpdate . I am evading this issue by placing a null check at the start of OnRoomPropertiesUpdate but I don't think this hacky "solution" will help me understand why this error happens.

Any guesses?

Comments

  • Eristen
    Options
    Bump?
  • Tobias
    Options
    In PUN 2, your code registers for callbacks but also has to unregister, when the callbacks are not needed anymore. So in your case, somehow the instance is not removed from the callback list.

    Check if your scripts override OnEnable and OnDisable if you inherit MonoBehaviourPunCallbacks. Make sure to call base for both.
  • Eristen
    Options
    I see. That's good to know. Unfortunately I already call the base for both magic methods, like this:
        public override void OnDisable()
        {
          base.OnDisable();
    
         // my code
        }
    
  • GTadeon
    GTadeon
    edited February 2023
    Options

    @Tobias Thank you!! I had the exact same issue but on a different callback method. I had a class (called CreateAndJoinRoomsSystem.cs attached to the CreateAndJoinRooms game object in the scene) that overrides OnRoomListUpdate method, and spent hours and hours trying to figure out why the heck the whole game object (CreateAndJoinRooms) is null when getting back in the lobby scene for the second time (on first load everything worked fine, which makes sense given that it was initial load), but now I understand. Indeed, manually calling the base.OnDisable in the CreateAndJoinRoomsSystem's OnDisable did the trick!

    The way I understand it, it happens because the PhotonHandler - because it persists across all scenes.

    So the game flow in my case was:

    loading => lobby (here everything works fine as it's initial load and PhotonHandler/LoadBalancignClient has yet be initialized) => game => lobby again => NOW HERE is where the issue happened in my case, because the PhotonHandler (which, once again , obviously persists across all scenes all the time, from the moment created) didn't dispose/clear/got rid of the old reference (the old "target", the so called "target" in LoadBalancingClient.cs , i.e. this one :


    ....

      public void OnRoomListUpdate(List<RoomInfo> roomList)

        {

          this.client.UpdateCallbackTargets();


          foreach (ILobbyCallbacks target in this)

          {

            Debug.Log("ZOVEM OnRoomListUpdate na targetu: " + target.GetType().ToString());

            target.OnRoomListUpdate(roomList);

          }

        }

    ....

    so it was essentially calling the target for which the game object does no longer exist.


    Anyway, calling base.OnDisable() in my consumer class (that is CreateAndJoinRoomsSystem in my case, in its OnDisable () method) resolved it , thank you!