Randomly run into this… (error continues to count up…)
UnityException: Load is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour ‘OnlineGameConnect’ on game object ‘Mission_Controll’.
See “Script Serialization” page in the Unity Manual for further details.
PhotonNetwork…cctor () (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:62)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for PhotonNetwork
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class OnlineGameConnect : Photon.MonoBehaviour {
public string GameClassic = "GameClassic";
public string GameReserch = "GameResearch";
public string GameType = "";
public string s = "";
public float WaitingForPlayers = 10;
public int PlayersInRoom;
public bool ConnectedToGame;
public GameObject SearchingForPlayers;
public GameObject GameMode;
public Text Timer;
public Text PlayerCount;
PhotonPlayer[] otherList = PhotonNetwork.otherPlayers;
//...........................................................................
void ConnectNow()
{
PhotonNetwork.ConnectUsingSettings("0.1");
}
//...........................................................................
public void OnJoinedLobby()
{
PhotonNetwork.JoinRandomRoom();
}
//...........................................................................
void OnPhotonRandomJoinFailed()
{
if (GameType == "Classic") {
RoomOptions roomOptions = new RoomOptions () { isVisible = false, MaxPlayers = 4 };
PhotonNetwork.JoinOrCreateRoom (GameClassic, roomOptions, TypedLobby.Default);
} else {
RoomOptions roomOptions = new RoomOptions () { isVisible = false, MaxPlayers = 4 };
PhotonNetwork.JoinOrCreateRoom (GameReserch, roomOptions, TypedLobby.Default);
}
}
//...........................................................................
void OnJoinedRoom (){
// when Inside a room....
ConnectedToGame = true;
SearchingForPlayers.SetActive (true);
GameMode.SetActive (false);
}
//...........................................................................
public void ClassicModeButton (){
GameType = "Classic";
ConnectNow ();
}
public void ReserchModeButton (){
GameType = "Reserch";
ConnectNow ();
}
//...........................................................................
void LateUpdate (){
Timer.text = WaitingForPlayers.ToString();
// how manny players are conected?....
foreach (PhotonPlayer player in otherList) {
s += player.ToString();
}
PlayerCount.text = s;
// if we are in the room (Connected,) Star tthe countdown!
if (ConnectedToGame == true) {
if (PhotonNetwork.isMasterClient) {
WaitingForPlayers -= 1 * Time.deltaTime;
} else {
// STREAM..........
}
// Count down, when countdown reachers 0, or you get 5 players, the game starts. :D
if (WaitingForPlayers < 0) {
if (GameType == "Classic") {
string[] zones = new string[3] { "ClassicA", "ClassicB", "ClassicC" };
int random = Random.Range (0, 3);
Application.LoadLevel (zones [random]);
} else {
string[] zones = new string[3] { "ResearchA", "ResearchB", "ResearchC" };
int random = Random.Range (0, 3);
Application.LoadLevel (zones [random]);
}
}
}
}
//...........................................................................
}