Search Unity

Third Party Photon - Joining/Leaving room

Discussion in 'Multiplayer' started by Cjreek, Jul 9, 2014.

  1. Cjreek

    Cjreek

    Joined:
    Apr 23, 2013
    Posts:
    33
    I wonder how I properly join a room and load a level and then leave the room and load the main menu level.
    When I'm leaving the game I'm getting this error:
    In my menu (level 0) theres a Join button:
    Code (csharp):
    1. if (GUI.Button(..., "Join Game"))
    2. {              
    3.      PhotonNetwork.JoinRoom(roomsList[i].name);
    4.      PhotonNetwork.LoadLevel(1);
    5. }
    in level 1 there's a gamecontroller-GO with a script attached:
    Code (csharp):
    1. void Update()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.Escape))
    4.     {
    5.             PhotonNetwork.DestroyPlayerObjects(PhotonNetwork.player);
    6.             PhotonNetwork.LeaveRoom();
    7.             PhotonNetwork.LoadLevel(0);
    8.     }
    9. }
    When i enter a room and then later hit Escape to leave the room and return to the main menu i get the warning/error above.

    I think I get this error for the gameobjects of the other players which where in the same room.
    But I can't use PhotonNetwork.DestroyAll() because that would kill the whole game.

    What am I missing?
     
  2. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    You shouldn't call LeaveRoom() and LoadLevel() after one another.

    LeaveRoom() needs some time to process since it has to travel to the server.
    Call LoadLevel in the OnLeftRoom callback that Photon calls. Simply create a

    Code (csharp):
    1. void OnLeftRoom()
    2. {
    3.     PhotonNetwork.LoadLevel(0);
    4. }
    function in the same class you are calling LeaveRoom() from
     
  3. Cjreek

    Cjreek

    Joined:
    Apr 23, 2013
    Posts:
    33
    You were about 1 day to late - I figured it out just yesterday ;)
    But thanks for your help nevertheless :)
     
  4. deyvos

    deyvos

    Joined:
    Nov 3, 2019
    Posts:
    1
    Thanks, oliver. It was very useful. I was supposed to call PhotonNetwork.InRoom in coroutine but I guessed that might be wrong.