PunEvent.OwnershipUpdate throws a null reference

Options
I noticed a null reference error when joining an existing room. I couldn't figure out why so I commented out all RPCs and eventually even removed the photon view components. The error seems to appear when I add a photon view component to anything in the scene. These objects are not brought in by instantiation, they exist in the scene already. the error is thrown when a user joins and loads the same scene with their own photon view object. I checked in the inspector and the photon view for the joiner shows it as belonging to the host master.

In PhotonNetworkPart.cs
in OnEvent()
In the PunEvent.OwnershipUpdate case
the line:

Player prevOwner = view.Owner;

throws a null reference error. I have added a check for view == null that triggers for each photon view in the joining scene.

After throwing this error, which I have converted to a warning with my if statement, the code seems to work as expected.

Am I using the photon view incorrectly? is there a way to specify that the computer loading the scene owns the photon views in that scene? I'm fairly sure this error did not occur the first few times I joining a room after updating but I can't figure out what I changed and seem to have eliminated everything except simply adding the component.

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @DF_WideCode,

    Thank you for choosing Photon!

    What PUN version are you using?

    Could you share the full stack trace to the NullReferenceException?
    The error seems to appear when I add a photon view component to anything in the scene. These objects are not brought in by instantiation, they exist in the scene already. the error is thrown when a user joins and loads the same scene with their own photon view object.
    Am I using the photon view incorrectly? is there a way to specify that the computer loading the scene owns the photon views in that scene?
    hmm why not add PhotonView to GameObject in the scene already?
    why do you need to add new PhotonView dynamically without PhotonNetwork.Instantiate?

  • Tobias
    Options
    This could be due to the Master Client knowing views that this player doesn't know yet.
    Do you use PUN's built in scene synchronization? Or do you at least pause dispatching incoming messages while you load the scene?
    Refer to Switching Scenes.
  • DF_WideCode
    Options
    I am using PUN2 version 2.34.1 and Unity 2021.1.16f1

    I will try to get the trace later (away from PC on work laptop).

    I may not have explained it properly. The objects are part of the scene and already have the photon view component attached before I run the program. The master loads the scene without issue but the next player to join throws the error. It looks like a ownership update event is created and the view does not have a previous owner.

    I am very new to PUN and only able to test multiplayer features at my home PC (with multiple monitors) so I can't say I am using any scene synchronization (unless it happens automatically) and I am not pausing messages while loading the scene.

    I will check out the link. Thank you
  • DF_WideCode
    Options
    PhotonNetwork.AutomaticallySyncScene = true;
    Fixed my issue

    Thank you.

    I probably should go through all the tutorials but where is the fun in that...
  • OneManArmy
    OneManArmy ✭✭✭
    edited August 2021
    Options
    I have the same problem after update to latest version. In v2.26 and earlier versions everything was fine.
    I just tested with AutomaticallySyncScene and it doesn't work in my game.
    But i tested in another project and i can confirm that AutomaticallySyncScene fixes this error.
    If you need i can prepare sample project.
    NullReferenceException: Object reference not set to an instance of an object
    Photon.Pun.PhotonNetwork.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2415)
    Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3356)
    ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PeerBase.cs:898)
    ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/EnetPeer.cs:565)
    ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PhotonPeer.cs:1863)
    Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:221)
    Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:147)