Photon connection problems in second try

Options
First I'm sorry for my English. I have a problem with the connection with photon, I have 2 scenes, the first with the menu and the second with the game. When I access the game scene, it connects to the photon and searches or creates a game. The first time I play everything runs smoothly but when I return to the menu and give play again stays in "Best region found in PlayerPrefs Connecting to: Eu" and does nothing more. I do not understand what I'm doing wrong. When I run it for the second time, the unity emulator lets me stop it but if I try to do something else it stops unity and I have to force it to close.
public void Connect()
    {
        Debug.Log("connect");
        if (PhotonNetwork.connectionState == ConnectionState.Disconnected)
        {
            if (previousRoom != null) {
                Debug.Log("con sala previa");
                PhotonNetwork.ReconnectAndRejoin();
            }
            else
            {
                Debug.Log("sin sala previa"); //enter here at the second try
                PhotonNetwork.ConnectUsingSettings(_gameVersion);
            }
        }
    }
//exit game scene methods
private IEnumerator fadeToMenu()
    {
        if (local != null)
            local.abandonar();
        animandofadeimg = true;
        fadeimg.transform.Find("Background").Find("reloj").GetComponent<Image>().enabled = false;
        fadeimg.transform.Find("Background").Find("looking_for_game").GetComponent<Text>().enabled = false;
        float alpha = fadeimg.GetComponent<CanvasGroup>().alpha;
        while (alpha < 1)
        {
            alpha += Time.deltaTime;
            if (alpha > 1)
                alpha = 1;
            fadeimg.GetComponent<CanvasGroup>().alpha = alpha;
            yield return null;
        }
        animandofadeimg = false;
        if (PhotonNetwork.inRoom)
        {
            Debug.Log("Manual Room Exit: Leaving room - " + PhotonNetwork.room.Name);
            if (PhotonNetwork.isMasterClient)
            {
                PhotonNetwork.DestroyAll();
            }
            PhotonNetwork.Disconnect();
            crearLog("desconecta de la sala");
        }
        loadMenu();
        yield return null;
    }

private void loadMenu()
    {
        if (!SceneManager.GetSceneByName("Menu").isLoaded)
        {
            SceneManager.LoadScene(1);
        }
        else
        {
            SceneManager.SetActiveScene(SceneManager.GetSceneByName("Menu"));
        }
        clearData();
    }

    private void clearData()
    {
        alpha = 0f;
        preguntas = null;
        if (sinresponder != null)
        {
            sinresponder.Clear();
            sinresponder = null;
        }
        preguntaActual = null;
        recompensa = 10;
        TextoPregunta.text = "";
        Puntos.text = "";
        Tiempo.text = "";
        Rivaltiempo.text = "";
        RivalPuntos.text = "";
        foreach (Button b in buttons)
        {
            b.GetComponentInChildren<Text>().text = "";
        }
        local = null;
        rival = null;
        escala = 0f;
        crecimiento = true;
        entrando = false;
        saliendo = false;
        ended = false;
        ocultando = true;
        animando = false;
        ganado = true;
        previousRoom = null;
        foreach (GameObject go in FindObjectsOfType<GameObject>())
            Destroy(go);
        Destroy(gameObject);
    }

Comments

  • Hi @viridesoft,

    the described scenario shouldn't be a problem at all and should work. When you return to the Menu scene, do you disconnect from Photon?

    To see if you can find the reason yourself you can add a breakpoint to the ConnectUsingSettings call and debug step-by-step through the code which gets called afterwards.
  • I put a lot of logs and cant see nothing. I load the new scene in loadMenu() and only in loadMenu(). LoadMenu only is called after call disconnect. I should wait to OnDisconnectedFromPhoton for loadMenu()?

  • Yes, you can try implementing OnDisconnectedFromPhoton callback and wait for it getting called. From this callback you can load the Menu scene. This makes sure, that you are completely disconnected before trying to connect again.
  • ok, tomorrow i try and tell you, txs
  • viridesoft
    edited November 2017
    Options
    the error continue, i only charge the menu scene when i'm full disconnected, here:
    public override void OnDisconnectedFromPhoton()
        {
            public override void OnDisconnectedFromPhoton()
        {
            if(local==null || local.isAbandonado())
            {
                Debug.Log("-------------loadmenu");
                loadMenu();
            }
            Debug.Log("-------------DISCONECTED");
        }
        }
  • I can't find a solution, here are my steps:
    -menu scene
    -go game scene
    -connect photon
    -join or create random room
    -game
    -end game
    -discconnect photon.
    -when discconected go menu scene
    -go game scene
    -stop after find best server eu.

    Any can help me please?
  • Please add breakpoints for the second attempt to connect and see where it gets stuck. I think you can start debugging at the beginning of the ConnectToRegionMaster function of the NetworkingPeer script.
  • Created the following two scripts to test this scenario myself based on the information you have shared. My tests were successful.

    MenuScene
    using UnityEngine;
    
    public class MenuScene : MonoBehaviour
    {
        public void OnGUI()
        {
            GUILayout.BeginVertical();
            GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
            GUILayout.Label("Menu Scene");
    
            if (GUILayout.Button("Go to Game Scene"))
            {
                UnityEngine.SceneManagement.SceneManager.LoadScene("GameScene");
            }
    
            GUILayout.EndVertical();
        }
    }
    GameScene
    using UnityEngine;
    
    public class GameScene : MonoBehaviour
    {
        public void OnGUI()
        {
            GUILayout.BeginVertical();
            GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
            GUILayout.Label("Game Scene");
    
            if (!PhotonNetwork.connected)
            {
                if (GUILayout.Button("Connect to Photon"))
                {
                    PhotonNetwork.autoJoinLobby = true;
                    PhotonNetwork.ConnectUsingSettings("v1.0");
                }
            }
            else
            {
                if (PhotonNetwork.inRoom)
                {
                    if (GUILayout.Button("End game and disconnect from Photon"))
                    {
                        PhotonNetwork.Disconnect();
                    }
                }
            }
    
            GUILayout.EndVertical();
        }
    
        public void OnJoinedLobby()
        {
            Debug.Log("Joined Lobby. Trying to join or create a room.");
    
            PhotonNetwork.JoinOrCreateRoom("Test Chamber", new RoomOptions(), null);
        }
    
        public void OnJoinedRoom()
        {
            Debug.Log("Joined Room.");
        }
    
        public void OnDisconnectedFromPhoton()
        {
            UnityEngine.SceneManagement.SceneManager.LoadScene("MenuScene");
        }
    }
    Note: Please stop creating multiple threads with the same topic as the time we need to answer will be increased and things might get confusing.
  • I use Photon.PunBehaviour instead of MonoBehaviour, can be the reason?
    I use Photon.PunBehaviour for control:
    -OnJoinedRoom();
    -OnPhotonRandomJoinFailed(); (for create if fail to join).
    -OnPhotonPlayerConnected(); (manage rival login).
  • I use Photon.PunBehaviour instead of MonoBehaviour, can be the reason?


    This shouldn't be a problem at all. You can try this yourself by adding some Debug Log to the OnJoinedRoom callback. If you see those logs in the console, it works fine.
  • viridesoft
    edited November 2017
    Options
    I find the problem, don't understand but I find. When I load the Menu Scene, after load, I call this method for clean game scene
    private void clearData()
        {
            alpha = 0f;
            preguntas = null;
            if (sinresponder != null)
            {
                sinresponder.Clear();
                sinresponder = null;
            }
            preguntaActual = null;
            recompensa = 10;
            TextoPregunta.text = "";
            Puntos.text = "";
            Tiempo.text = "";
            Rivaltiempo.text = "";
            RivalPuntos.text = "";
            foreach (Button b in buttons)
            {
                b.GetComponentInChildren<Text>().text = "";
            }
            local = null;
            rival = null;
            escala = 0f;
            crecimiento = true;
            entrando = false;
            saliendo = false;
            ended = false;
            ocultando = true;
            animando = false;
            ganado = true;
            previousRoom = null;
            /*foreach (GameObject go in FindObjectsOfType<GameObject>())
                Destroy(go);*/
            Destroy(gameObject);
        }

    The for each bucle, if I don't comment, the game fails and if I comment all work ok. I put it to remove all the objects from the server for an old warning and it broke the code,I do not know why.

  • /*foreach (GameObject go in FindObjectsOfType())
    Destroy(go);*/


    This removes each available game object in the scene including the one with the attached script used for connection.