PhotonNetwork.connected is false after reloading a scene

Options
This is weird, I have these menu buttons you cant click unless PhotonNetwork.connected is true but I can use them after reloading the scene , you may think I made a mistake and my networkmanager is destroyed after loading the scene but no, I used Dontdestroyonload() and also I have a GUI Label that uses PhotonNetwork.connectionState.ToString() to tell me the state of the connection and Im connected the whole time

Debug.Log(PhotonNetwork.connected); returns false after reloading the scene (for example by loading the "character menu" scene and loading the main menu scene again)

Menu
[code2=csharp]using UnityEngine;
using System.Collections;

public class MainMenu : MonoBehaviour {
private int MainMenuId = 1;
private Rect MainMenuRect = new Rect(Screen.width * 0.33f, Screen.height * 0.60f, Screen.width / 3, Screen.height * 0.4f);
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
Debug.Log(PhotonNetwork.connected);
}
void OnGUI(){
GUI.skin = xGraphicsManager.Instance.GameGUI;
GUILayout.Label(PhotonNetwork.connectionState.ToString());
GUI.DrawTexture(new Rect (0, 0, Screen.width, Screen.height),xGraphicsManager.Instance.GamePadBackGround);
GUI.Window(MainMenuId, MainMenuRect, MainMenuFunc, "");

}

void MainMenuFunc(int id){

if (PhotonNetwork.connected){
if (GUILayout.Button("Find Match")){
Application.LoadLevel("Loby");

}
if (GUILayout.Button("Create Match")){
Application.LoadLevel("Match Creator");

}
}
if (!PhotonNetwork.connected) {
GUILayout.Label("Find Match");
GUILayout.Label("Create Match");
}
if (GUILayout.Button("Player Options")) {
Application.LoadLevel("Character Menu");

}
if (GUILayout.Button("Exit")) {
Application.Quit();

}


}
}[/code2]


Network Manager
[code2=csharp]using UnityEngine;
using System.Collections;

public class NetworkManager : MonoBehaviour {
public GameObject Player;
public Camera _Camera;
public RoomInfo[] RoomList;
private string PlayerName = "Player Name";
// Use this for initialization
void Start () {
DontDestroyOnLoad(this);
PhotonNetwork.ConnectUsingSettings("v1.0");

}
void OnJoinedRoom() {
PhotonNetwork.isMessageQueueRunning = false;
Application.LoadLevel("LoadingScene");
}
void OnReceivedRoomListUpdate() {
RoomList = PhotonNetwork.GetRoomList();
}
public void InitializePlayer() {
GameObject _Player = PhotonNetwork.Instantiate(Player.name, new Vector3(100, 1, 90), Quaternion.identity, 0);
_Player.GetComponent<PlayerProfile>().PlayerID = _Player.GetComponent<PhotonView>().ownerId;
_Camera = Camera.main;
_Camera.GetComponent<CameraScript>().Player = _Player;
_Camera.GetComponent<CameraScript>().enabled = true;
PhotonNetwork.playerName = PlayerName;
}

}[/code2]

Comments

  • You should use PhotonNetwork.LoadLevel() instad of Application.LoadLevel() :)
  • wilczek013 wrote:
    You should use PhotonNetwork.LoadLevel() instad of Application.LoadLevel() :)

    Thanks for the answer but PhotonNetwork.connected still returns false after using PhotonNetwork.LoadLevel
  • I used a workaround:

    I used
    [code2=csharp]if (PhotonNetwork.connectionState.ToString() != "Connected")[/code2]

    instead of

    [code2=csharp]if (PhotonNetwork.connected)[/code2]

    but I still would like to know why this happens