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 /
This question was closed Feb 19, 2016 at 05:26 AM by jmparavicini for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by jmparavicini · Feb 19, 2016 at 04:59 AM · c#editorwindownot-working

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

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
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
avatar image Bunny83 · Feb 19, 2016 at 06:29 AM 0
Share

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

Answers Answers and Comments

97 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 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 avatar image avatar image avatar image avatar image avatar image

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


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