- Home /
The question is answered, right answer was accepted
Custom Editor Window not opening after layout crash
Hi Guys, So I made an EditorScript which lets me make items but thats not relevant, the point is that yesterday everything worked just fine and today when I opened Unity a message appeared which said that the layout system crashed or something similar. When the project opened, everyrtime I tried to open the Custom Editor Window I was getting the following 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. ItemCreator..ctor ()
Here is my script:
using UnityEngine;
using UnityEditor;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEditor.Events;
public class ItemCreator : EditorWindow
{
string m_ItemName;
float m_AttackDamage;
float m_BlastDamage;
float m_AttackSpeed;
float m_Armor;
float m_BlastResist;
float m_MovementSpeed;
bool m_HasActive;
Vector2 m_ScrollPos;
StoreBehaviour m_StoreBehaviour = GameObject.FindGameObjectWithTag("Store").GetComponent<StoreBehaviour>();
[MenuItem("Item Creator/Create Item #i")]
public static void ShowWindow()
{
ItemCreator window = (ItemCreator)EditorWindow.GetWindow (typeof (ItemCreator));
window.Show();
}
void OnGUI()
{
m_ScrollPos = GUILayout.BeginScrollView(m_ScrollPos, false, false);
GUILayout.Label("Main Settings", EditorStyles.boldLabel);
m_ItemName = EditorGUILayout.TextField("Item Name", m_ItemName);
m_AttackDamage = EditorGUILayout.FloatField("Attack Damage", m_AttackDamage);
m_BlastDamage = EditorGUILayout.FloatField("Blast Damage", m_BlastDamage);
m_AttackSpeed = EditorGUILayout.FloatField("Attack Speed", m_AttackSpeed);
m_Armor = EditorGUILayout.FloatField("Armor", m_Armor);
m_BlastResist = EditorGUILayout.FloatField("Blast Resistance", m_BlastResist);
m_MovementSpeed = EditorGUILayout.FloatField("Movement Speed", m_MovementSpeed);
m_HasActive = EditorGUILayout.BeginToggleGroup("Has this Item actives", m_HasActive);
m_MovementSpeed = EditorGUILayout.FloatField("Movement Speed", m_MovementSpeed);
EditorGUILayout.EndToggleGroup();
if (GUILayout.Button("Create Item", GUILayout.Width(100f)))
{
GameObject m_NewItem = new GameObject(m_ItemName);
m_NewItem.transform.parent = GameObject.FindGameObjectWithTag("ItemsParent").transform;
m_NewItem.AddComponent<CanvasRenderer>();
m_NewItem.AddComponent<Image>();
m_NewItem.AddComponent<Button>();
//m_NewItem.GetComponent<Button>().onClick.AddListener(() => GameObject.FindGameObjectWithTag("Store").GetComponent<StoreBehaviour>().SelectItem(m_NewItem));
//m_NewItem.GetComponent<Button>().onClick.(delegate {GameObject.FindGameObjectWithTag("Store").GetComponent<StoreBehaviour>().SelectItem(m_NewItem);});
// UnityAction action = new UnityAction(Func);
UnityAction m_Action;
// m_Action = new UnityAction(GetAction);
m_Action = new UnityAction(m_StoreBehaviour.Test);
UnityEventTools.AddPersistentListener(m_NewItem.GetComponent<Button>().onClick, m_Action);
m_NewItem.AddComponent<ItemStats>();
}
EditorGUILayout.EndScrollView();
}
void GetAction()
{
Debug.Log("Wat");
}
}
The error points me to line 27. Now The window doesn't even open it just gives me an error but the worst part is that the script works in a new project. I tried many different things like reseting the editor layout changing the syntax of line 27 and 28 but nothing worked
Please help me guys I need to get this working
thanks in advance to the saviour xD, skullbeats1
Answer by jmparavicini · Feb 19, 2016 at 05:26 AM
I Found out what it was, the error was pointing me to the wrong part of the code. the problem was where i was assigning the StoreBehaviour.
It didn't really pointed to the wrong place, you might just have read it wrong ^^. You didn't copy the whole error with the full stacktrace, however you even copied that part:
ItemCreator..ctor ()
".ctor" is the internal name for the constructor or a class. The error also tells you that you are calling a function when declaring a variable. It even guessed right that you use it either in the constructor or a field initializer. In your case you used "FindGameObjectWithTag" in a field initializer.
Things that should be executed when the window is created should be placed in the OnEnable callback
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Boolean is set to true but is unchecked in the inspector 1 Answer
How can you use Reflection to create an object editor window 1 Answer
OnGui elements as objects? 2 Answers