PhotonNetwork.ConnectUsingSettings(gameVersion) failing.

On my multiplayer menu, I should click a button saying "Connect". When I click this, I get a NullReferenceException error.
I went into the code of Photon, and I looked for the reason. I ran a debug line that checks if networkingPeer is null, and it was, which explains the nullreference exception, the thing is, I don't know why it is null.

Please help since I have a very tight timeline and I need to get this out!

Comments

  • This is the first case I read about with networkingPeer being null.

    Do you try to access it directly? Are there other exceptions first?
    Can you describe how you used it? And provide the exception log?

    You can look at the demos from the PUN package to see our default usage.
  • No, I am not accessing it directly. I am simply using PhotonNetwork.ConnectUsingSettings("DevBuild6c"), nothing else.

    Here's an exact copy of the log:
    NullReferenceException: Object reference not set to an instance of an object
    PhotonNetwork.ConnectUsingSettings(System.String gameVersion) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:1072)
  • public static bool ConnectUsingSettings(string gameVersion)
    {
    if (PhotonServerSettings == null)
    {
    Debug.LogError("Can't connect: Loading settings failed. ServerSettings asset must be in any 'Resources' folder as: " + serverSettingsAssetFile);
    return false;
    }

    if(networkingPeer == null)
    Debug.LogError("null");
    networkingPeer.SetApp(PhotonServerSettings.AppID, gameVersion);

    if (PhotonServerSettings.HostType == ServerSettings.HostingOption.OfflineMode)
    {
    offlineMode = true;
    return true;
    }

    if (offlineMode)
    {
    Debug.LogWarning("ConnectToMaster() disabled the offline mode. No longer offline.");
    }

    offlineMode = false; // Cleanup offline mode
    isMessageQueueRunning = true;
    networkingPeer.IsInitialConnect = true;

    if (PhotonServerSettings.HostType == ServerSettings.HostingOption.SelfHosted || !PhotonNetwork.UseNameServer)
    {
    networkingPeer.IsUsingNameServer = false;
    networkingPeer.MasterServerAddress = PhotonServerSettings.ServerAddress + ":" + PhotonServerSettings.ServerPort;
    return networkingPeer.Connect(networkingPeer.MasterServerAddress, ServerConnection.MasterServer);
    }

    return networkingPeer.ConnectToRegionMaster(PhotonServerSettings.Region);
    }

    Here is the code for ConnectUsingSettings, with an added Debug.LogError() in it.
  • I can only guess what's wrong, as the cause for the nullref is somewhere else.
    By design, the static constructor of the PhotonNetwork class will initialize the networkingPeer. If that fails, then it should log some error or so but I can't reproduce that.

    I would try to re-import PUN.
    You could also create a new and empty project, import PUN there and test if that runs. Then close Unity to replace the PUN files by Explorer or Finder.
    The relevant files should be:
    \Assets\Photon Unity Networking\Plugins
    \Assets\Photon Unity Networking\Editor
    \Assets\Photon Unity Networking\UtilityScripts
    \Assets\Plugins\*photon*.*

    If the problem persists, send me the project or at least the full log.
  • Sorry the project exceeds 5 GB. Where exactly do I find the full log?

    Also, when I start my project, I get these three errors in my console:
    "Load can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function."

    "ArgumentException: Load can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    PhotonNetwork..cctor()
    Rethrow as TypeInitializeException: An exception was thrown by the type initializer for PhotonNetwork
    RoomOptions..cctor()
    MainMenu...ctor()"

    "ArgumentException: Load can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    PhotonNetwork..cctor()
    Rethrow as TypeInitializeException: An exception was thrown by the type initializer for PhotonNetwork
    RoomOptions..cctor()
    MainMenu...ctor()"

    Does this have any relevance?

    Thanks
  • Here is MainMenu.cs:
    using UnityEngine;
    using System.Collections;

    public class MainMenu : Photon.MonoBehaviour {

    public GUISkin skin;

    public MovieTexture menuVid;

    private bool settingsMenu;
    private bool graphicsMenu;
    private bool controlsMenu;
    private bool multiplayerMenu;
    private bool showStatus = false;
    private bool showDefault = true;
    private bool showRoomDetails;
    private bool hostMenu;
    private bool joinMenu;
    private bool showHostStatus = false;
    private bool justPinging = false;
    private bool fullscreenBoolean = false;

    public int maxPlayers = 6;

    RoomOptions newRoomOptions = new RoomOptions();

    //Temporary setting values
    private float meshTreeDistValue;
    private float foliageDistValue;
    private float detailDistanceValue;

    private string inputRoomName = "";
    private string status = "Idle";
    private string hostStatus = "Idle";

    void Start () {
    Debug.Log(PhotonNetwork.connected.ToString());

    menuVid.Play();
    menuVid.loop = true;

    meshTreeDistValue = UserSettings.meshTreeDistance;
    foliageDistValue = UserSettings.foliageDistance;
    detailDistanceValue = UserSettings.detailDistance;

    if(UserSettings.fullscreen == 0){
    fullscreenBoolean = false;
    } else {
    fullscreenBoolean = true;
    }
    }

    void OnGUI () {
    GUI.skin = skin;
    GUI.Box(new Rect (Screen.width /2 - 125, Screen.height - 60, 250, 50), "Music: Dark Frog by Kevin MacLeod");
    if(showDefault){
    if (GUI.Button (new Rect (Screen.width /2 - 415, 30, 200, 85), "Singleplayer")) {
    PhotonNetwork.offlineMode = true;
    PlayerPrefs.SetString("Room Name", "");
    Application.LoadLevel(1);
    }
    if(GUI.Button (new Rect(Screen.width /2 - 205, 30, 200, 85), "Multiplayer")){
    PhotonNetwork.offlineMode = false;
    multiplayerMenu = true;
    showDefault = false;
    }
    if(GUI.Button (new Rect (Screen.width /2 + 5, 30, 200, 85), "Options")){
    showDefault = false;
    settingsMenu = true;
    }
    if(GUI.Button (new Rect (Screen.width /2 + 215, 30, 200, 85), "Exit Game")){
    Application.Quit ();
    }
    }

    if (settingsMenu) {
    if (GUI.Button (new Rect(Screen.width /2 - 100, Screen.height /2 - 120, 200, 50), "Controls")) {
    settingsMenu = false;
    controlsMenu = true;
    } else if(GUI.Button (new Rect(Screen.width /2 - 100, Screen.height /2 - 60, 200, 50), "Graphics")){
    settingsMenu = false;
    graphicsMenu = true;
    } else if(GUI.Button (new Rect(Screen.width /2 - 80, Screen.height /2 + 60, 160, 50), "Close")){
    settingsMenu = false;
    showDefault = true;
    }
    }

    if(graphicsMenu){
    if(GUI.Button (new Rect(Screen.width /2 - 475, Screen.height /2 - 60, 450, 50), "Shadows: " + UserSettings.shadows_statusText)){
    if(UserSettings.shadows == 0){
    GameObject scriptsObj = GameObject.Find("Camera");
    UserSettings sun = scriptsObj.GetComponent<UserSettings>();
    sun.setShadowMode(1);
    } else if (UserSettings.shadows == 1){
    GameObject scriptsObj = GameObject.Find("Camera");
    UserSettings sun = scriptsObj.GetComponent<UserSettings>();
    sun.setShadowMode(2);
    } else if (UserSettings.shadows == 2){
    GameObject scriptsObj = GameObject.Find("Camera");
    UserSettings sun = scriptsObj.GetComponent<UserSettings>();
    sun.setShadowMode(0);
    }
    }
    if(GUI.Button (new Rect(Screen.width /2 - 475, Screen.height /2, 450, 50), "Texture Quality: " + UserSettings.textureQuality_statusText)){
    if(UserSettings.textureQuality == 0){
    UserSettings.setTextureQuality(1);
    } else if (UserSettings.textureQuality == 1){
    UserSettings.setTextureQuality(2);
    } else if (UserSettings.textureQuality == 2){
    UserSettings.setTextureQuality(3);
    } else if (UserSettings.textureQuality == 3){
    UserSettings.setTextureQuality(0);
    }
    }
    if(GUI.Button (new Rect(Screen.width /2 - 475, Screen.height /2 + 60, 450, 50), "vSync: " + UserSettings.vSync_statusText)){
    if(UserSettings.vSync == 0){
    UserSettings.setVSync(1);
    } else if (UserSettings.vSync == 1){
    UserSettings.setVSync(0);
    }
    }
    if(GUI.Button (new Rect(Screen.width /2 - 475, Screen.height /2 - 120, 450, 50), "Item Bobbing: " + UserSettings.itemBobbing_statusText)){
    if(UserSettings.itemBobbing == 0){
    UserSettings.setItemBobbing(1);
    } else if (UserSettings.itemBobbing == 1){
    UserSettings.setItemBobbing(0);
    }
    }
    if(GUI.Button (new Rect(Screen.width /2 - 475, Screen.height /2 -180, 450, 50), "Screen resolution: " + UserSettings.resolutionX.ToString() + "x" + UserSettings.resolutionY.ToString())){
    if(UserSettings.resolutionX == 1920){
    UserSettings.setResolution(800, 600);
    } else if (UserSettings.resolutionX == 800){
    UserSettings.setResolution(1024, 768);
    } else if (UserSettings.resolutionX == 1024){
    UserSettings.setResolution(1360, 768);
    } else if (UserSettings.resolutionX == 1360){
    UserSettings.setResolution(1366, 768);
    } else if (UserSettings.resolutionX == 1366){
    UserSettings.setResolution(1920, 1080);
    } else {
    UserSettings.setResolution(Screen.width, Screen.height);
    }
    }
    if(GUI.Button (new Rect(Screen.width /2 + 25, Screen.height /2 - 180, 450, 50), "Fullscreen: " + fullscreenBoolean.ToString())){
    if(fullscreenBoolean == false){
    fullscreenBoolean = true;
    UserSettings.toggleFullscreen(fullscreenBoolean);
    } else if(fullscreenBoolean == true){
    fullscreenBoolean = false;
    UserSettings.toggleFullscreen(fullscreenBoolean);
    }
    }
    //Add new button above here

    //Sliders:
    GUI.Box(new Rect (Screen.width /2 + 25, Screen.height /2, 200, 25), "Mesh tree distance: " + UserSettings.meshTreeDistance.ToString("F0"));
    meshTreeDistValue = GUI.HorizontalSlider(new Rect (Screen.width /2 + 25, Screen.height /2 + 30, 200, 20), meshTreeDistValue, 5, 400);
    UserSettings.setMeshTreeDistance(meshTreeDistValue);

    GUI.Box(new Rect (Screen.width /2 + 25, Screen.height /2 - 120, 200, 25), "Foliage distance: " + UserSettings.foliageDistance.ToString("F0"));
    foliageDistValue = GUI.HorizontalSlider(new Rect (Screen.width /2 + 25, Screen.height /2 - 90, 200, 20), foliageDistValue, 0, 250);
    UserSettings.setFoliageDistance(foliageDistValue);

    GUI.Box(new Rect (Screen.width /2 + 25, Screen.height /2 - 60, 200, 25), "Detail distance: " + UserSettings.detailDistance.ToString("F0"));
    detailDistanceValue = GUI.HorizontalSlider(new Rect (Screen.width /2 + 25, Screen.height /2 - 30, 200, 20), detailDistanceValue, 5, 500);
    UserSettings.setDetailDistance(detailDistanceValue);

    //Add new slider above here

    //BACK FROM GRAPHICS MENU BUTTON
    if(GUI.Button (new Rect(Screen.width /2 - 60, Screen.height / 2 + 120, 120, 50), "Back")){
    graphicsMenu = false;
    settingsMenu = true;
    }
    }

    if(hostMenu && !showHostStatus){
    inputRoomName = GUI.TextField(new Rect(Screen.width /2 - 125, Screen.height /2 - 15, 250, 30), inputRoomName);
    if(inputRoomName.Length > 0){
    if(GUI.Button (new Rect(Screen.width /2 - 100, Screen.height /2 + 20, 200, 75), "Create room")){
    Connect ();
    showHostStatus = true;
    }
    }
    if(GUI.Button (new Rect(Screen.width /2 - 100, Screen.height /2 + 105, 200, 75), "Back")){
    hostMenu = false;
    multiplayerMenu = true;
    }
    }

    if(hostMenu && showHostStatus){
    GUI.Box(new Rect (Screen.width /2 - 100, Screen.height /2 - 30, 200, 60), hostStatus);
    if(GUI.Button (new Rect(Screen.width /2 - 100, Screen.height - 100, 200, 30), "Cancel")){
    StopCoroutine("timeout");
    PhotonNetwork.LeaveLobby();
    PhotonNetwork.Disconnect();
    showHostStatus = false;
    }
    }

    if(joinMenu && !showStatus){
    inputRoomName = GUI.TextField(new Rect (Screen.width /2 - 125, Screen.height /2 - 15, 250, 30), inputRoomName);
    if(inputRoomName.Length > 0){
    if (GUI.Button (new Rect(Screen.width /2 - 100, Screen.height /2 + 20, 200, 75), "Connect")){
    Connect();
    showStatus = true;
    StartCoroutine(timeout (12));
    }
    }
    if(GUI.Button (new Rect(Screen.width /2 - 100, Screen.height /2 + 105, 200, 75), "Back")){
    joinMenu = false;
    multiplayerMenu = true;
    }
    }

    if(multiplayerMenu){
    if(GUI.Button (new Rect(Screen.width /2 - 100, Screen.height /2 + 10, 200, 50), "Host")){
    hostMenu = true;
    multiplayerMenu = false;
    }
    if(GUI.Button (new Rect(Screen.width /2 - 100, Screen.height /2 + 60, 200, 50), "Join")){
    joinMenu = true;
    multiplayerMenu = false;
    }
    if(GUI.Button (new Rect(Screen.width /2 - 100, Screen.height /2 + 180, 200, 50), "Back")){
    multiplayerMenu = false;
    showDefault = true;
    }
    }

    if (joinMenu && showStatus && !showRoomDetails){
    GUI.Box(new Rect (Screen.width /2 - 100, Screen.height /2 - 30, 200, 60), status);
    if(GUI.Button (new Rect(Screen.width /2 - 100, Screen.height - 100, 200, 30), "Cancel")){
    PhotonNetwork.LeaveLobby();
    PhotonNetwork.Disconnect();
    showStatus = false;
    }
    }

    if(showRoomDetails){
    if(GUI.Button(new Rect( Screen.width /2 - 500, Screen.height /2 + 60, 200, 50), "Back")){
    showRoomDetails = false;
    showStatus = false;
    PhotonNetwork.LeaveRoom();
    PhotonNetwork.LeaveLobby();
    PhotonNetwork.Disconnect();
    }
    if(GUI.Button(new Rect( Screen.width /2 + 300, Screen.height /2 + 60, 200, 50), "Join")){
    PlayerPrefs.SetString("Room Name", inputRoomName);
    PlayerPrefs.SetInt("HostOrClient", 0);
    PlayerPrefs.Save ();
    Application.LoadLevel(1);
    }
    GUI.Box (new Rect(Screen.width /2 - 500, Screen.height /2 - 250, 1000, 250), "Room found\n\n" + PhotonNetwork.room.name + "\nPlayer count: " + (PhotonNetwork.room.playerCount -1).ToString() + "/" + PhotonNetwork.room.maxPlayers.ToString());
    }

    if(controlsMenu){
    showDefault = true;
    controlsMenu = false;
    }
    }


    void Connect (){
    Debug.Log ("connecting");
    status = "Connecting to master server...";
    hostStatus = "Connecting to master server...";
    PhotonNetwork.ConnectUsingSettings("DevBuild6b");
    PhotonNetwork.JoinLobby ();
    Debug.Log ("joining lobby");
    status = "Reaching network...";
    hostStatus = "Creating host...";
    }

    public void OnJoinedLobby () {
    if(joinMenu){
    Debug.Log ("joined lobby. we are in the join menu");
    PhotonNetwork.JoinRoom(inputRoomName);
    status = "Joining room: " + inputRoomName;
    }
    else if(hostMenu){
    Debug.Log ("joined lobby. we are in the host room, lets ping to see if name exists already");
    //Let's ping with a server and see if it exists. If not, we create it.
    justPinging = true;
    PhotonNetwork.JoinRoom(inputRoomName);
    StartCoroutine(timeout(4));
    }
    }

    public void OnJoinedRoom (){
    if(joinMenu)
    showRoomDetails = true;
    }

    public void startHost () {
    PlayerPrefs.SetString("Room Name", inputRoomName);
    PlayerPrefs.SetInt("HostOrClient", 1);
    PlayerPrefs.Save ();
    Application.LoadLevel(1);
    }

    IEnumerator timeout (int i) {
    yield return new WaitForSeconds(i);
    if(!PhotonNetwork.inRoom && !justPinging){
    status = "Unable to connect:\nRoom doesn't exist / Server full";
    PhotonNetwork.LeaveLobby();
    }
    if(justPinging){
    if(PhotonNetwork.inRoom){
    PhotonNetwork.LeaveRoom();
    PhotonNetwork.LeaveLobby();
    PhotonNetwork.Disconnect();
    status = "There is already a room by that name! Please pick a different, unique room name.";
    justPinging = false;
    } else {
    justPinging = false;
    newRoomOptions.maxPlayers = maxPlayers;
    PhotonNetwork.CreateRoom(inputRoomName, newRoomOptions, TypedLobby.Default);
    startHost();
    }
    }
    }

    }


    WARNING: Script may contain dead ends, please ignore those. Those are planned to be completed later in development.
  • I fixed the problem! It was a very simple and also invisible error. At the top of MainMenu.cs I say "RoomOptions newRoomOptions = new RoomOptions();"
    That was the mistake.
    I changed it to:
    "RoomOptions newRoomOptions;

    void Start () {
    newRoomOptions = new RoomOptions();
    }

    Thank you for helping me out and trying! I really appreciate it! If anyone else comes by this issue, the solution is as it is above this line. Thanks again! :D