Getting a missing reference exception on recreating or rejoining a room

So i'm building a basic card game and i have provided a button using which player can exit the game. The problem is when the other players exit the game as well and create another room to play again all players get a missing reference exception that few objects were destroyed. however those so called destroyed objects are clearly there and visible in the inspector. This is the error

MissingReferenceException: The object of type 'GameObject' 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.
GameManager.AssignCardsToPlayer (UnityEngine.GameObject player, System.String[] cards) (at Assets/Scripts/GameManager.cs:673)
GameManager.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Scripts/GameManager.cs:805)
Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3018)
ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at <11e9abbca912444aa80ed58a280369fc>:0)
ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at <11e9abbca912444aa80ed58a280369fc>:0)
ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at <11e9abbca912444aa80ed58a280369fc>:0)
Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:205)
Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:139)

I'm pretty new to photon and i'm guessing it has something to do with the user not properly exiting the room. Am i missing something here? Any help is appreciated.

This is the exit logic on the quit button:
private IEnumerator DisconnectPlayer()
{
PhotonNetwork.Disconnect();
while (PhotonNetwork.IsConnected)
yield return null;
SceneManager.LoadScene(0);
}

This is the logic for connecting to a room (if relevant)
public void JoinRoom()
{
if (PhotonNetwork.IsConnected)
{
ExitGames.Client.Photon.Hashtable expectedCustomRoomProperties = new ExitGames.Client.Photon.Hashtable() { { "bootAmount", bootAmount } };
PhotonNetwork.JoinRandomRoom(expectedCustomRoomProperties,0);
}
}

public override void OnJoinRandomFailed(short returnCode, string message)
{
roomName = "room_" + Guid.NewGuid().ToString();
SetMsg("Joining failed. Creating new room");
RoomOptions options = new RoomOptions();
options.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable() { { "bootAmount", bootAmount } };
options.CustomRoomPropertiesForLobby = new string[] { "bootAmount" };
options.MaxPlayers = 6;
options.PublishUserId = true;
options.CleanupCacheOnLeave = true;
PhotonNetwork.CreateRoom(roomName, options, TypedLobby.Default);
}

Answers

  • So i found the issue. It was a silly mistake actually. I forgot to unsubscribe from "PhotonNetwork.NetworkingClient.EventReceived" event while leaving the room.