Player still in room

If I create a room, leave the room and then create a room, the player is still there and didn't leave. The gameobject is just there. I think I made a mistake when creating the room, maybe it is not getting cleared properly?

This is my Join and Create room script:
public void createNewRoom()
{
if (photonButtons.createRoomInput.text.Length >= 1)
{
PhotonNetwork.CreateRoom(photonButtons.createRoomInput.text, new RoomOptions() { MaxPlayers = 20 }, null);
}
else
{
invalidRoomNameCreate.SetActive(true);
Debug.Log("Roomname needs to be atleast more then 1 character");
}
}

public void joinOrCreateRoom()
{
if (photonButtons.joinRoomInput.text.Length >= 1)
{
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = 20;
PhotonNetwork.JoinRoom(photonButtons.joinRoomInput.text);
PhotonNetwork.DestroyAll();
} else
{
invalidRoomNameJoin.SetActive(true);
Debug.Log("Roomname needs to be atleast more then 1 character");
}
}

Comments

  • Hi @KarMaPlaYzz,

    is there any other function where you are creating a room except the one in the code snippet above? If so, please check if you use the EmptyRoomTtl and / or PlayerTtl property of the RoomOptions in this case. Those values describe how long an empty room is kept on the server and how long a client is marked as inactive in the room before the server removes him from this room.

    Also check if you are using PhotonNetwork.autoCleanUpPlayerObjects = false; somewhere in your code. This one prevents the server from cleaning up objects of players which have left the room.
  • edited August 9
    This is the whole photonHandler script:
    maybe this will help you out

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Networking;
    using UnityEngine.SceneManagement;

    public class photonHandler : MonoBehaviour {

    public photonButtons photonButtons;

    public Transform spawnpoint;

    public GameObject mainPlayer;

    public GameObject invalidRoomNameJoin;
    public GameObject invalidRoomNameCreate;
    public GameObject roomExists;
    public GameObject roomDoesntExist;

    private void Awake()
    {
    menuS = FindObjectOfType();

    PhotonNetwork.automaticallySyncScene = true;
    PhotonNetwork.autoCleanUpPlayerObjects = true;

    DontDestroyOnLoad(this.transform);

    SceneManager.sceneLoaded += OnSceneFinishedLoading;

    RoomInfo[] rooms = PhotonNetwork.GetRoomList();
    }

    public void createNewRoom()
    {
    if (photonButtons.createRoomInput.text.Length >= 1)
    {
    PhotonNetwork.CreateRoom(photonButtons.createRoomInput.text, new RoomOptions() { MaxPlayers = 20 }, null);
    }
    else
    {
    invalidRoomNameCreate.SetActive(true);
    Debug.Log("Roomname needs to be atleast more then 1 character");
    }
    }

    public void joinOrCreateRoom()
    {
    if (photonButtons.joinRoomInput.text.Length >= 1)
    {
    RoomOptions roomOptions = new RoomOptions();
    roomOptions.MaxPlayers = 20;
    PhotonNetwork.JoinRoom(photonButtons.joinRoomInput.text);
    } else
    {
    invalidRoomNameJoin.SetActive(true);
    Debug.Log("Roomname needs to be atleast more then 1 character");
    }
    }

    public void moveScene()
    {
    PhotonNetwork.LoadLevel("Game Scene");
    }

    private void OnJoinedRoom()
    {
    moveScene();
    Debug.Log("We are connected to the room");
    }

    private void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
    {
    if(scene.name == "Game Scene")
    {
    SpawnPlayer();
    }
    }

    private void SpawnPlayer()
    {
    PhotonNetwork.Instantiate(mainPlayer.name, mainPlayer.transform.position, mainPlayer.transform.rotation, 0);
    Debug.Log("SpawnPlayer");
    }

    private Menu menuS;

    /*public void DisconnectFromRoom()
    {
    menuS.menuOverlay.SetActive(false);
    menuS.optionsOverlay.SetActive(false);

    PhotonNetwork.LeaveRoom();
    }*/

    public void OnPhotonPlayerDisconnected(PhotonPlayer player)
    {
    Debug.Log("Removing objects");

    PhotonNetwork.LoadLevel("Main Menu");
    }
    }
  • Are you sure, that this is the same object when you enter another room? If you already have a script attached to the object you are network instantiating, please add a Debug.Log to it's Awake or Start function, where you print the InstanceId to the console. You can get this by calling GetInstanceID(); on a MonoBehaviour object. Please check if the same InstanceId is logged to the console twice. If there are two different Ids logged, everything is fine. If there is just one log in the console showing an InstanceId, the object doesn't get destroyed. In this case check, if you are leaving the room properly. You can do this by using the OnLeftRoom callback.
  • edited August 9
    I created a room: -9702 ID
    I left,
    I created a new room (I get 2 ID's): -15760, -16128

    prntscr.com/kgqogz
  • And what do I need to write in the OnLeftRoom?
  • edited August 14
    And what do I need to write in the OnLeftRoom?


    You can add a simple Debug.Log there, just to make sure, that the client is leaving the room correctly. Instead of using PhotonNetwork.LeaveRoom(); please try using PhotonNetwork.LeaveRoom(false);. This makes sure that the client leaves the room immediately instead of becoming inactive. This furthermore forces the server to remove the Instantiation call. If this issue still persists, please check how many times PhotonNetwork.Instantiate is called. Please also check how often NetworkingPeer.OnEvent is called, especially how often the PunEvent.Instantiation case is processed.

    I created a new room (I get 2 ID's): -15760, -16128


    Do you use another name for the new room? If not, please try using another name when creating the second room, just to make sure, it really is a new room on the server as well.

    Please let me also know, which PUN and which Unity version you are currently working with.
  • Am I spawning the player correctly?
    Am I creating the rooms correctly?

    OnLeftRoom(); is being called and I tried running PhotonNetwork.LeaveRoom(false); in there, but still the GameObject is not removed.
    Do you use another name for the new room?

    I've tried using a new name, and the same name, still no progress.

    Please let me also know, which PUN and which Unity version you are currently working with.

    I'm using the latest version of PUN, Unity 2018.2.3f (64-bit).
  • Is the instantiated object correctly destroyed when leaving the room? You can check this in the scene hierarchy.

    Is there any chance, that you can create a repro case for me, so that I can have a closer look at the issue?
Sign In or Register to comment.