- Home /
Question by
Desination-X · Aug 07, 2014 at 02:22 PM ·
multiplayerphotonnullreferenceexceptionconnection
PhotonNetwork.ConnectUsingSettings("DevBuild6b") returns NullReferenceException
I have a main menu script that also routes to multiplayer. It was working fine, but then out of the blue it decided it should start giving me the error "NullReferenceException" for PhotonNetwork.ConnectUsingSettings("DevBuild6b")
Although I don't understand what could possibly be going wrong, I checked my code over and over, and there was still nothing that could cause it! Does anyone know why?
Here is my MainMenu script (also includes the connection stuff):
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 () {
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.Label(new Rect(5, 5, 200, 30), PlayerPrefs.GetInt("Fullscreen").ToString());
GUI.Label(new Rect(5, 35, 200, 30), fullscreenBoolean.ToString());
GUI.Label(new Rect(5, 65, 200, 30), UserSettings.fullscreen.ToString());
GUI.Label(new Rect(5, 95, 200, 30), Screen.fullScreen.ToString());
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")) {
PlayerPrefs.SetString("Room Name", "");
Application.LoadLevel(1);
}
if(GUI.Button (new Rect(Screen.width /2 - 205, 30, 200, 85), "Multiplayer")){
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")){
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;
}
}
public void Connect (){
status = "Connecting to master server...";
hostStatus = "Connecting to master server...";
PhotonNetwork.ConnectUsingSettings("DevBuild6b");
PhotonNetwork.JoinLobby ();
status = "Reaching network...";
hostStatus = "Creating host...";
}
public void OnJoinedLobby () {
if(joinMenu){
PhotonNetwork.JoinRoom(inputRoomName);
status = "Joining room: " + inputRoomName;
}
else if(hostMenu){
//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(5);
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();
}
}
}
}
Comment