- Home /
PhotonUnityNetworking UnityException
Hello I am using PhotonUnityNetworking for the multiplayer in my new game. However for some reason I am getting the error
UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. UnityEngine.Object.DestroyImmediate (UnityEngine.Object obj) PhotonNetwork.CreateSettings () (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2973) PhotonNetwork..cctor () (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:1065) Rethrow as TypeInitializationException: An exception was thrown by the type initializer for PhotonNetwork RoomOptions..ctor () NetworkManager..ctor ()
Here is the code for my NetworkManager where I think the error is coming from.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class NetworkManager : MonoBehaviour {
public int WhichRoomOption = 0;
RoomOptions Circuit_Race_Room_Option = new RoomOptions() { isOpen = true, isVisible = true, maxPlayers = 8 };
RoomOptions Aerial_Warfare_Room_Option = new RoomOptions() { isOpen = true, isVisible = true, maxPlayers = 8 };
RoomOptions Naval_Battles_Room_Option = new RoomOptions() { isOpen = true, isVisible = true, maxPlayers = 2 };
RoomOptions Motorbike_Escapade_Room_Option = new RoomOptions() { isOpen = true, isVisible = true, maxPlayers = 2 };
void Awake ()
{
DontDestroyOnLoad(gameObject);
}
void Start ()
{
PhotonNetwork.ConnectUsingSettings("v1.0.0");
}
public void CircuitRaceButtonClicked ()
{
WhichRoomOption = 1;
Debug.Log("Circuit Race Button Clicked" + WhichRoomOption);
}
public void AerialWarfareButtonClicked ()
{
WhichRoomOption = 2;
Debug.Log("Aerial Warfare Button Clicked" + WhichRoomOption);
}
public void NavalBattlesButtonClicked ()
{
WhichRoomOption = 3;
Debug.Log("Naval Battles Button Clicked" + WhichRoomOption);
}
public void MotorbikeEscapadeButtonClicked ()
{
WhichRoomOption = 4;
Debug.Log("Motorbike Escapade Button Clicked" + WhichRoomOption);
}
void Update ()
{
Debug.Log("Joined the Lobby");
switch (WhichRoomOption)
{
case 1 :
PhotonNetwork.JoinRoom("CircuitRace");
Application.LoadLevel("MultiplayerWaitingRoom");
Debug.Log("Joined CircuitRace Room and going to MultiplayerWaitingRoom");
break;
case 2 :
PhotonNetwork.JoinRoom("Aerial_Warfare");
Application.LoadLevel("MultiplayerWaitingRoom");
Debug.Log("Joined Aerial_Warfare Room and going to MultiplayerWaitingRoom");
break;
case 3 :
PhotonNetwork.JoinRoom("Naval_Battles");
Application.LoadLevel("MultiplayerWaitingRoom");
Debug.Log("Joined Naval_Battles Room and going to MultiplayerWaitingRoom");
break;
case 4 :
PhotonNetwork.JoinRoom("Motorbike_Escapade");
Application.LoadLevel("MultiplayerWaitingRoom");
Debug.Log("Joined Motorbike_Escapade Room and going to MultiplayerWaitingRoom");
break;
}
}
void OnPhotonRandomJoinFailed ()
{
Debug.Log("Random Join Failed, Creating Room");
switch (WhichRoomOption)
{
case 1 :
PhotonNetwork.CreateRoom("CircuitRace", Circuit_Race_Room_Option, null);
Application.LoadLevel("MultiplayerWaitingRoom");
Debug.Log("Creating Circuit Race Room");
break;
case 2 :
PhotonNetwork.CreateRoom("Aerial_Warfare", Aerial_Warfare_Room_Option, null);
Application.LoadLevel("MultiplayerWaitingRoom");
Debug.Log("Creating Aerial Warfare Room");
break;
case 3 :
PhotonNetwork.CreateRoom("Naval_Battles", Naval_Battles_Room_Option, null);
Application.LoadLevel("MultiplayerWaitingRoom");
Debug.Log("Creating Naval Battles Room");
break;
case 4 :
PhotonNetwork.CreateRoom("Motorbike_Escapade", Motorbike_Escapade_Room_Option, null);
Application.LoadLevel("MultiplayerWaitingRoom");
Debug.Log("Creating Motorbike Escapade Room");
break;
default :
Debug.Log("no room created!");
break;
}
}
void OnJoinedRoom ()
{
Debug.Log("OnJoinedRoom");
}
void OnLevelWasLoaded (int level)
{
if (level == 6)
{
switch (WhichRoomOption)
{
case 1 :
if (PhotonNetwork.countOfPlayersInRooms == 8)
{
Application.LoadLevel("CircuitRace");
Debug.Log("Started CircuitRace");
}
break;
case 2 :
if (PhotonNetwork.countOfPlayersInRooms == 8)
{
Application.LoadLevel("Aerial_Warfare");
Debug.Log("Started Aerial_Warfare");
}
break;
case 3 :
if (PhotonNetwork.countOfPlayersInRooms == 2)
{
Application.LoadLevel("Naval_Battles");
Debug.Log("Started Naval_Battles");
}
break;
case 4 :
if (PhotonNetwork.countOfPlayersInRooms == 2)
{
Application.LoadLevel("Motorbike_Escapade");
Debug.Log("Motorbike_Escapade");
}
break;
}
}
}
}
Im sure its something simple but I have no idea why its throwing up this error around 100 times a second. I have tried deleting the script and creating it again but that didn't fix the problem. Anyone else know of a solution? Another question is why is it that in the Void Update section, seemly the switch doesn't occur, as I don't get the debug.log messages that I should get but I do get the Debug.Log("Joined the Lobby") message
Thanks Dok
Your answer
![](https://koobas.hobune.stream/wayback/20220612044226im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add Turn Based to Photon Pun 1 Answer
Photon with many objects 0 Answers
How to change colors in unity using photon? 0 Answers
Photon Cloud, Multiplayer Damage script 0 Answers