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 JoshuaMcKenzie · Jan 21, 2016 at 02:53 AM · nullreferenceexceptionguistyleproject settingsrevert

EditorStyles was overridden, can I revert back?

So I was creating a tool and I wanted to have a group of buttons behave like tabs.

       GUIContent content = new GUIContent();
         GUIStyle MiniLeft = EditorStyles.miniButtonLeft;
         GUIStyle MiniMid = EditorStyles.miniButtonMid;
         GUIStyle MiniRight = EditorStyles.miniButtonRight;
         GUILayoutOption[] buttonWidth =  new GUILayoutOption[]
         {
             GUILayout.MinWidth (50),
             GUILayout.ExpandWidth (false),
             
         };
         
 public void OnGUI()
 {        
     GUILayout.BeginHorizontal ();
         content.text = "Enemies";
         content.tooltip="Edit enemies that will spawn in this level";
         MiniLeft.fontStyle = (TabState == 0)?FontStyle.Bold:FontStyle.Normal;
         if (GUILayout.Button (content,MiniLeft, buttonWidth)) 
         {
             GUI.FocusControl("");
             TabState = 0;
         }
         
         
         content.text = "Defenses";
         content.tooltip="Edit what defenses you can build this level";
         MiniMid.fontStyle = (TabState == 1)?FontStyle.Bold:FontStyle.Normal;
         if (GUILayout.Button (content,MiniMid, buttonWidth)) 
         {
             GUI.FocusControl("");
             TabState = 1;
         }
         
         
         content.text = "Spell";
         content.tooltip="Edit what spells you can cast this level";
         MiniRight.fontStyle = (TabState == 2)?FontStyle.Bold:FontStyle.Normal;
         if (GUILayout.Button (content,MiniRight, buttonWidth)) 
         {
             GUI.FocusControl("");
             TabState = 2;
         }
     GUILayout.EndHorizontal ();

     TabState = Mathf.Clamp(TabState,0,2);

     subWindows[TabState].OnGUI();
 }

I assumed a line like GUIStyle MiniLeft = EditorStyles.miniButtonLeft; would simply get me a copy of the style as a new instance (similar to Vector3.one;), but the documention didn't clarify this. I've promptly switched my code to GUIStyle MiniLeft = new GUIStyle (EditorStyles.miniButtonLeft); however it seems the damage as already been done, it directly altered the style settings. so every time when I render the window I get errors like this:

 NullReferenceException: Object reference not set to an instance of an object
 UnityEditor.EditorStyles.get_miniButtonLeft () (at C:/buildslave/unity/build/Editor/Mono/GUI/EditorStyles.cs:69)
 ChamberEditor..ctor ()
 UnityEditorInternal.InternalEditorUtility:LoadSerializedFileAndForget(String)
 UnityEditor.WindowLayout:LoadWindowLayout(String, Boolean)


I get roughly 6 to 10 copies of that error every time the OnGUI is called and the window blanks out. I assume its a file that needs to be deleted/reverted in the Project settings and let unity remake it but I'm unsure exactly which. I would really prefer not to delete the entire folder cause resetting other settings like collision layers becomes a chore. I am using Unity Pro version 5.1.1f1

So the question is: Is there a way to revert back to the default EditorStyles settings?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by JoshuaMcKenzie · Jan 21, 2016 at 10:30 AM

I found what the issue was.

I had my custom styles defined and initialized outside the OnGUI call, as class variables. The class is a ScriptableObject Asset (its SubAssets has similar stylings) and the GUIStyles were with the [NonSerialized] flag( the class is [Serializable], though not sure if that matters). After running some breakpoints (and getting some sleep) I saw that my custom styles were null even though the Editor styles they copied from weren't.

Setting them in OnEnable doesn't work as I assume it should, but re-initalizing the styles inside the OnGUI fixed the issue. It messes with my OCD that I'm recreating the same objects more than once in a single class instance but it seems that is how it must be.

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 Daniel-Everland · Oct 02, 2018 at 04:54 PM 0
Share

Hello Joshua,

I think I have a solution that'll satisfy your OCD

 private void SceneGUI()
 {
     EditorGUILayout.TextArea("test", Styles.TextAreaStyle);
 }
 private static class Styles
 {
     public static GUIStyle TextAreaStyle;
 
     static Styles()
     {
         TextAreaStyle = new GUIStyle(EditorStyles.textArea);
         TextAreaStyle.normal = EditorStyles.label.normal;
     }
 }

As you can see this will not initialize the styles until they're needed, and will only do so once. No need for messy initializations :)

avatar image
1

Answer by Bunny83 · Jan 21, 2016 at 04:38 AM

Restart the Unity editor.

The comparison with "Vector3.one" doesn't hold since GUIStyle is a class while Vector3 is a struct. It will indeed return the reference to the GUIStyle object in memory. When you alter it you can't revert it back unless you saved the values before you applied your changes.

However as said, no harm done. If you restart the editor, Unity will reload it's internal GUIStyles. Just makes sure you don't have some code around that again would mess up the internal styles.

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 JoshuaMcKenzie · Jan 21, 2016 at 10:25 AM 0
Share

There's no longer any instance of the original EditorStyle being overridden in the project. I've restarted several times and i would still get these errors.

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

33 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

Related Questions

NullreferenceException, BeginScrollView style change 3 Answers

NullReference with trigger C# ? 1 Answer

Null Reference Excepting :Object reference not set to an instance of object while changing text of 3D text 2 Answers

NullReference on textfield 1 Answer

Can I Ignore Null Reference Exceptions? ( bug? ) 3 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