Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dok101 · Nov 07, 2015 at 09:02 AM · c#errormultiplayerphoton

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add Turn Based to Photon Pun 1 Answer

[Photon] Looking for example code for late joining clients syncing instantiated game objects that don't exist in the base scene 0 Answers

Photon with many objects 0 Answers

How to change colors in unity using photon? 0 Answers

Photon Cloud, Multiplayer Damage script 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges