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
1
Question by $$anonymous$$ · May 15, 2017 at 03:20 AM · editor-scripting

EditorUtility.SaveFilePanel and BeginHorizontal cause "Object reference not set..."

I have some code like the following, that is giving me an error at the second BeginHorizontal. Does anyone know why? Does the call to SaveFilePanel cause problems for EditorGUILayout? The error only occurs if I click on the 'Save as JSON' button.

 m_scrollPos2 = EditorGUILayout.BeginScrollView(m_scrollPos2);
 EditorGUILayout.BeginHorizontal();   
 if (GUILayout.Button(new GUIContent("Save as JSON", "save JSON to file"))) {
     EditorUtility.SaveFilePanel("Save data",Application.persistentDataPath, "data", "json");
 }
 EditorGUILayout.EndHorizontal();
 EditorGUILayout.BeginHorizontal(); // error here
 ...
 EditorGUILayout.EndHorizontal();
 ...
 EditorGUILayout.EndScrollView();

The traceback is like:

 NullReferenceException: Object reference not set to an instance of an object

 UnityEngine.GUILayoutUtility.BeginLayoutGroup (UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options, System.Type layoutType) (at /home/builduser/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:296)

 UnityEditor.EditorGUILayout.BeginHorizontal (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at /home/builduser/buildslave/unity/build/Editor/Mono/EditorGUI.cs:7234)

 UnityEditor.EditorGUILayout.BeginHorizontal (UnityEngine.GUILayoutOption[] options) (at /home/builduser/buildslave/unity/build/Editor/Mono/EditorGUI.cs:7214)

(I should add that there are many questions here about "Object reference not set" errors that are about user object references that are null. In this case, the failure -- see the backtrace above -- is in Unity's code).

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
3
Best Answer

Answer by Bunny83 · May 15, 2017 at 04:54 AM

Yes, the SavePanel (as well as some other rare methods) will break the current layout stack because they start a new layout stack. You have to call EditorGUIUtility.ExitGUI(); after you called SavePanel. ExitGUI will raise a special exception (ExitGUIException ) that will immediately terminate the current GUI execution. This exception is silently caught by Unity.

EditorGUIUtility.ExitGUI() is not documented. However it has been suggested several times by the Unity staff in the forum. Also it's used in the editor code of any built-in Editor \ EditorWindow that would otherwise suffer from this problem.

Other cases are:

  • opening an EditorWindow from inside OnGUI

  • using a ColorField, CurveField, ObjectField will throw an ExitGUIException because they open an EditorWindow when you click the control

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 $$anonymous$$ · May 15, 2017 at 01:42 PM 0
Share

That works for me thanks.

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

73 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

Related Questions

How to change settings on editor using Enum with an array? 0 Answers

Detect Play mode about to be entered 3 Answers

Bug with displaying components in Inspector after hideFlags.NotEditable? 1 Answer

Detecting when a component has been pasted onto an inactive object 1 Answer

BCE0044 erro only shows when trying to build .apk 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