Destroyed Photon MasterClient or Event Error?

Options
Guys, could you help me to solve a little issue? I'm getting this error here:
MissingReferenceException: The object of type 'LobbyPlayerNetwork' 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.
UnityEngine.Component.GetComponent[PhotonView] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ComponentBindings.gen.cs:48)
PhotonView.Get (UnityEngine.Component component) (at Assets/ExternalAssets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:673)
Photon.MonoBehaviour.get_photonView () (at Assets/ExternalAssets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonClasses.cs:488)
LobbyPlayerNetwork.SvOnPlayerDisconnect (.PhotonPlayer player) (at Assets/_EonWar/Scripts/_Refactored/Lobby/LobbyPlayerNetwork.cs:168)
ConnectionManager.OnPhotonPlayerDisconnected (.PhotonPlayer otherPlayer) (at Assets/_EonWar/Scripts/_Refactored/Network/ConnectionManager.cs:118)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/ExternalAssets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2721)
NetworkingPeer:HandleEventLeave(Int32, EventData) (at Assets/ExternalAssets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1371)
NetworkingPeer:OnEvent(EventData) (at Assets/ExternalAssets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2461)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
PhotonHandler:Update() (at Assets/ExternalAssets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:157)
I guess you guys should probably know the contest though.. So, I have this room, and in order for me to receive this error I need to switch the master client twice. What I do is, I load one build and the application on the editor, host on the editor, join on the build, exit on the editor making the build the master, then join on the editor again and exit on the build. Some more context:

ConnectionManager, it inherits from PunBehavior and has a static event for the following event
public override void OnPhotonPlayerDisconnected(PhotonPlayer otherPlayer)
{
base.OnPhotonPlayerDisconnected(otherPlayer);
if (OnPlayerDisconnect != null)
OnPlayerDisconnect.Invoke(otherPlayer);
}
LobbyPlayerNetworked (probably the bad guy somewhere), starts listening to ConnectionManager events on start and stops listening on destroy, no news here. The line that throws the error is the RPC line:
public void SvOnPlayerDisconnect(PhotonPlayer player)
{
if (!PhotonNetwork.isMasterClient)
return;
var slot = lobbyCanvas.GetSlotByOwner(player);
slot.SetOwner(null, null);
slot.UpdateState(LobbyPlayerState.Bot);
// So far nothing referencing the script is ever set or accessed
photonView.RPC("RpcUpdateSlot", PhotonTargets.AllViaServer, lobbyCanvas.GetSlotIndex(slot), slot.ToSerialized());
}
It would make sense for me if the error weren't coming from photon classes, I guess this is related to the rpc in some way, but not sure how...

Edit1: No RPC calls are buffered at this stage

Comments

  • Hi @JoaoBorks,

    MissingReferenceException: The object of type 'LobbyPlayerNetwork' has been destroyed but you are still trying to access it.


    Can you tell me which objects have this component attached? It looks like either the entire object or the component gets destroyed maybe due to some scene loading? Can you confirm that there isn't any scene loading and the object with this attached component still exists?
  • JoaoBorks
    JoaoBorks
    edited November 2017
    Options
    Hey @Christian_Simon, there is no scene loading at this stage and this object belongs to a player. Now what I'm thinking is, once the player leaves the room, the objects may still store a reference to it.. I'll double check that, hold on.. Nope, just checked and confirmed everyone removes the reference to a player who leaves. I guess this event is being sent to the player that was the host and left the match, but then its photonView is no longer available. I'm thinking of what to do if such case is true, I already tried to check for a null photonView, but it doesn't work.

    Edit: This function SvOnPlayerDisconnect won't ever print anything on the console, even if I remove the master server verification. Just checked with breakpoints and that's because once the code reaches this function, the this reference is null, so it can't even print to the console. Now, I don't really know why is this happening, this is probably my mistake somewhere

    Edit2: Do you know what would really help? An described event order from when a host leaves the game, in photon events. What happens first and what happens last? This way I can correctly handle the internal stuff. I'm currently listening to the following events:
    void OnPhotonPlayerDisconnected()
    void OnMasterClientSwitched()
    void OnLeftRoom()
  • You have stated that the script LobbyPlayerNetwork starts listening to events from the ConnectionManager. Before LobbyPlayerNetwork gets destroyed (and it gets destroyed according the error message) do you remove the listener(s) again? If you don't do this, you maybe want to implement Unity's OnDestroy callback and do it inside that function to really make sure, that you don't have that reference any longer.
  • JoaoBorks
    Options
    Yes, surely, but maybe it's not correctly handled:
    void SvOnDestroy()
    {
    if (!PhotonNetwork.isMasterClient)
    return;
    ConnectionManager.OnPlayerDisconnect -= SvOnPlayerDisconnect;
    }
    I don't know if at this stage the host is no longer the master client, so that's probably what's causing the issue
  • Have you already tried removing the PhotonNetwork.isMasterClient condition?