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 RustedBucket · Jun 21, 2011 at 04:58 PM · editor-scriptingeditorwindowongui error

Closing an editor window causes OnGui error

I have a simple editor script that I want to run from an EditorWindow but am running into a minor problem:

I have an OnGUI function that displays a string and two buttons. When you click the first button it will run a function and then close the window. When you click the second button it will just close the window.

Everything functions normally but after the first button runs it's function and closes I get an "ArgumentException: You can only call GUI functions from inside OnGUI". However the line it brings me to is inside an OnGUI call. Is there something I'm missing? Here is the OnGUI function that runs from my class that inherits from EditorWindow.

 void OnGUI()
 {
     GUILayout.Label("Save current scene in order to continue!", EditorStyles.boldLabel);
     
     if(GUILayout.Button("Yes"))
     {
         bool shouldContinue = EditorApplication.SaveScene(EditorApplication.currentScene);
         if(shouldContinue)
         {
             Foo();
             this.Close();
         }
     }
     
     if(GUILayout.Button("No")) //this is where the console error brings me if I hit the Yes button
     {    
         this.Close();
     }
 }
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

· Add your reply
  • Sort: 
avatar image
1

Answer by sneftel · Jun 21, 2011 at 05:47 PM

Put a return after each this.Close call. After you've closed the window, you shouldn't do any further GUIing.

Comment
Add comment · Show 5 · 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 RustedBucket · Jun 21, 2011 at 05:59 PM 0
Share

I've tried this and have gotten a "NullReferenceException: Object reference not set to an instance of an object

UnityEditor.DockArea.EndOffsetArea() at ..... Editor/$$anonymous$$ono/GUI/DockArea.cs 726)

UnityEditor.DockArea.OnGUI() at ....... Editor/$$anonymous$$ono/GUI/DockArea.cs654)"

avatar image sneftel · Jun 21, 2011 at 06:01 PM 0
Share

Strange. Are you sure there isn't extra code in that function you're not showing?

avatar image RustedBucket · Jun 21, 2011 at 06:30 PM 0
Share

Foo() is a function that opens up all my scenes one by one and runs a script that assigns persistent Unique ID's to objects then saves the level before switching to the next scene. It doesn't run any OnGUI related code.

avatar image sneftel · Jun 21, 2011 at 06:36 PM 0
Share

It's possible that some of the loading/saving stuff going on is causing UnityGUI to get confused. Here's a possible workaround: Ins$$anonymous$$d of running Foo() directly from OnGUI, ins$$anonymous$$d have a boolean in the class, initially set to false, which is set to true ins$$anonymous$$d of calling Foo() from OnGUI. Then, have the OnDestroy function check if that boolean is true, and if it is, then it runs Foo(). That way it doesn't happen during GUI drawing.

avatar image RustedBucket · Jun 21, 2011 at 06:49 PM 0
Share

It runs into the same problems even if I run Foo() from OnDestroy(). I'm willing to let it slide for now as the error doesn't actually break anything and is only run from in Editor, Foo() still performs perfectly with the expected results. The error is just an eyesore that I would like to get rid of. Thanks for your help so far though.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

SetReplacementShader in Editor Utility? 0 Answers

Accessing OnSceneGUI with EditorWindow in JS 1 Answer

Drawing a line in a EditorWIndow? 1 Answer

Editor Script with for loop 1 Answer

Is there a way to get all editable fields of a component in a script? 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