UnityException: You are not allowed to call this function when declaring a variable.

So this isn’t a game breaking error, however i would like these errors to be gone by the time we build and currently this is the only one and I’m not sure what to do with it. The script is set up to put an inventory GUI on an active camera. the game we’re making is akin to old school survival horrors with the fixed camera system like Fatal Frame, and the old Resident Evil games. So if someone can point out a better way to do this I would be greatly appreciative.

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.
PlayerMapInventory…ctor ()
UnityEngine.GameObject:AddComponent()
ActivateTriggerCamera:OnTriggerEnter(Collider) (at Assets/Scripts/ActivateTriggerCamera.cs:39)

enter code hereusing UnityEngine;
using System.Collections;

public class ActivateTriggerCamera : MonoBehaviour 
{
	#region Editor Variables
	
	public GameObject TriggerCamera = null;
	public bool DebugScript = false;
	
	private GameObject CurrentMainCamera = null;
	
	#endregion
	
	#region Public Methods
	
	public void OnTriggerEnter(Collider other)
	{
		if(other.tag == "Player")
		{
			if(CurrentMainCamera == null)
			{
				CurrentMainCamera = GameObject.FindWithTag("MainCamera");
				CurrentMainCamera.tag = "Untagged";
				CurrentMainCamera.SetActive(false);
				PlayerMapInventory inventory = CurrentMainCamera.gameObject.GetComponent<PlayerMapInventory>();
				if(inventory == null)
				{
					Destroy(inventory);
				}

				CurrentMainCamera = null;
				
				TriggerCamera.tag = "MainCamera";
				TriggerCamera.SetActive(true);
				inventory = TriggerCamera.gameObject.GetComponent<PlayerMapInventory>();
				if(inventory == null)
				{
					TriggerCamera.gameObject.AddComponent<PlayerMapInventory>();
				}
			}
			else
			{
				if(DebugScript == true)
				{
					Debug.Log("The current main camera is the same as the trigger's camera.");
				}
			}
		}
		else
		{
			if(DebugScript == true)
			{
				Debug.Log("Something other than the player entered " + gameObject.name + ".");
			}
		}
	}
	
	#endregion
}

here’s the script it’s calling. again i don’t get this error until after i start the game but everything works as intended. So yea i’m at a loss of words as to why it’s happening.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerMapInventory : MonoBehaviour 
{
	private GameObject[] cameras = GameObject.FindGameObjectsWithTag("MainCamera");
	public Texture inventoryBG;
	public Texture mapBG;
	public Texture[] Item = null;
	public GUISkin Inventory;
	private bool isInventoryGUIActive = false;
	private bool isMapGUIActive = false;

	static private Dictionary<int, string> inventoryNameDictionary = new Dictionary<int, string>()
	{
		{0, string.Empty},
		{1, string.Empty},
		{2, string.Empty},
		{3, string.Empty},
	};

	ItemClass itemObject = new ItemClass();
	// Use this for initialization
	void Start () 
	{

	}
	
	// Update is called once per frame
	void Update() 
	{

		//calling the inventory menu
		if(Input.GetKeyUp(KeyCode.I)) // keycode for inventory
		{
			if(isInventoryGUIActive == false) //if the inventory GUI is not active
			{
				isInventoryGUIActive = true; // set it to active
				isMapGUIActive = false; //deactivate map
				if(Time.timeScale == 1.0f) //if the game is in progress
					Time.timeScale = 0.0f; //stop the game
			}
			else if (isInventoryGUIActive == true) // if the inventory is active
			{
				isInventoryGUIActive = false; // set it to inactive
				Time.timeScale = 1.0f; // and continue the game.
			}
				
		}
		if(Input.GetKeyUp(KeyCode.M))
		{
			if(isMapGUIActive == false) //if the inventory GUI is not active
			{
				isMapGUIActive = true; // set it to active
				isInventoryGUIActive = false; //deactivate inventory
				if(Time.timeScale == 1.0f) //if the game is in progress
					Time.timeScale = 0.0f; //stop the game
			}
			else if (isMapGUIActive == true) // if the inventory is active
			{
				isMapGUIActive = false; // set it to inactive
				Time.timeScale = 1.0f; // and continue the game.
			}
			
		}

	}
	void OnGUI()
	{
		//inventory menu
		if(isInventoryGUIActive)
		{
			GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), inventoryBG);
			inventoryWindowMethod();
		}

		//Map
		if(isMapGUIActive)
		{
			GUI.DrawTexture (new Rect(0, 0, Screen.width, Screen.height), mapBG, ScaleMode.ScaleToFit);
		}
	}
	void inventoryWindowMethod ()
	{	
		//inventory items
		GUI.BeginGroup (new Rect (Screen.width / 10, Screen.height / 15, Screen.width, Screen.height));
	
		GUILayout.BeginArea (new Rect (0, 0, Screen.width / 2, Screen.height / 1.25f));
	
		GUI.Box (new Rect (0, 0, Screen.width / 2, Screen.height / 1.315f), " ");
	
		GUILayout.BeginHorizontal ();
		GUILayout.Button (inventoryNameDictionary[0], GUILayout.Height (Screen.height / 3));
		GUILayout.Button (inventoryNameDictionary[1], GUILayout.Height (Screen.height / 3));
		GUILayout.EndHorizontal ();
	
		GUILayout.BeginHorizontal ();
		GUILayout.Button (inventoryNameDictionary[2], GUILayout.Height (Screen.height / 3));
		GUILayout.Button (inventoryNameDictionary[3], GUILayout.Height (Screen.height / 3));
		GUILayout.EndHorizontal ();
	
		GUILayout.EndArea ();
	
		GUI.EndGroup ();
	
		//interact icons
		GUI.BeginGroup (new Rect (Screen.width / 1.65f, Screen.height / 15, Screen.width, Screen.height));
		GUI.Box (new Rect (0, 0, Screen.width / 3, Screen.height / 5.5f), "group2");
		GUILayout.BeginArea (new Rect (0, 5, Screen.width / 3, Screen.height / 1.315f));
	
	
		GUILayout.BeginHorizontal ();
		GUILayout.Button ("Look", GUILayout.Height (Screen.height / 6));
		GUILayout.Button ("Merge", GUILayout.Height (Screen.height / 6));
		GUILayout.Button ("Use", GUILayout.Height (Screen.height / 6));
		GUILayout.EndHorizontal ();
	
		GUILayout.EndArea ();
		GUI.EndGroup ();
	
		//show larger item icon
		GUI.BeginGroup (new Rect (Screen.width / 1.65f, Screen.height / 3.5f, Screen.width, Screen.height));
		GUI.Box (new Rect (0, 0, Screen.width / 3, Screen.height / 5.5f), "Inventory Image");
	
		GUI.EndGroup ();
	}
}

Your problem is line 7 of your PlayerMapInventory.

  private GameObject[] cameras = GameObject.FindGameObjectsWithTag("MainCamera");

The initialization needs to be moved into Start(). It is complaining because you are calling GameObject.FindGameObjectWithTag() outside of any C# method. Note this line is a bit puzzling to me. Typically there is only one main camera in the scene, and you can get access with ‘Camera.main’. If there is only one camera, then you don’t need an array of cameras.