Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NullReferenceException on 'GetStyle' Suddenly

Discussion in 'Immediate Mode GUI (IMGUI)' started by ashul, Oct 19, 2018.

  1. ashul

    ashul

    Joined:
    Aug 8, 2013
    Posts:
    40
    Hi everyone, I'm building a custom Editor Script using custom GUIStyles, and everything was going just fine. I had a custom GUIStyle with nice button textures, custom fonts, font sizes, etc... and I was attempting to add a scrollview and then everything disappeared. I removed the scrollview and everything still disappeared with the error of NullReferenceException:

    SlimUIError.PNG

    It worked before, and I didn't change anything. It studdenly fails to 'GetStyle' on 'slimUiGUiSkin' because when I change it back to 'EditorStyles.label' it works fine. This is the Project View for the path:

    SlimEditorPath.PNG

    I simplified the code and here it is:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine.UI;
    3.  
    4. public class UIEditor: EditorWindow {
    5.  
    6.     GUISkin slimUiGUiSkin;
    7.     GUIStyle slimUiButton;
    8.  
    9.  
    10.     [MenuItem("Window/SlimUI/UI Editor")]
    11.     public static void LoadBuilder(){
    12.         GetWindow<UIEditor>("SlimUI Editor");
    13.     }
    14.  
    15.     void Awake(){
    16.         slimUiGUiSkin = Resources.Load<GUISkin>("Assets/SlimUI/GUISkin/SUISkin.guiskin");
    17.         //slimUiGUiSkin = (GUISkin)AssetDatabase.LoadAssetAtPath("Assets/SlimUI/GUISkin/SlimUISkin.guiskin", typeof(GUISkin));
    18.  
    19.         slimUiButton = new GUIStyle(slimUiGUiSkin.GetStyle("button"));
    20.     }
    21.  
    22.     void OnGUI(){
    23.         GUILayout.Label("Builder", slimUiGUiSkin.GetStyle("largeTitle"));
    24.     }
    25. }
    26.  
    Does anyone know why this is happening and how I can get it to see the GUISkin again? Thanks!
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Because editor code isn't the most predictable in terms of the order it runs in, I usually setup all my styles in a single function (with null checks, etc) and call that style function first in my OnGui or OnInspector functions.
     
    ashul likes this.
  3. ashul

    ashul

    Joined:
    Aug 8, 2013
    Posts:
    40
    That's interesting. I didn't know that! How would I call them properly? And by call the style do you mean just the style or also the buttons?
     
  4. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    This is A solution (there are others)...

    Code (csharp):
    1.  
    2. public class UIEditor : EditorWindow
    3. {
    4.      protected GUIStyle myStyle;
    5.  
    6.      public void InitStyles()
    7.      {
    8.           if(myStyle == null)
    9.                myStyle = new GUIStyle(); //make your style here
    10.      }
    11. }
    12.  
    ...then...

    Code (csharp):
    1.  
    2. public class MyOtherEditorWindow : UIEditor
    3. {
    4.      public void OnGUI()
    5.      {
    6.           InitStyles();
    7.  
    8.           GUILayout.Label("blahblahblah", myStyle);
    9.      }
    10. }
    11.  
     
    andycodes likes this.
  5. ashul

    ashul

    Joined:
    Aug 8, 2013
    Posts:
    40
    I combined both of these into the single Editor Script and it works! After initializing everything in the first Public Class it ended up finding the GUISkin and Styles. Here is my code for anyone else who comes across this issue:


    Code (CSharp):
    1. public class UIEditor : EditorWindow
    2. {
    3.      protected GUISkin slimUiGUiSkin;
    4.      protected GUIStyle slimUiButton;
    5.      public void InitStyles()
    6.      {
    7.          slimUiGUiSkin = (GUISkin)AssetDatabase.LoadAssetAtPath("Assets/SlimUI/GUISkin/SlimUISkin.guiskin", typeof(GUISkin));
    8.    
    9.             if(slimUiGUiSkin == null)
    10.                 slimUiGUiSkin = new GUISkin();
    11.  
    12.             slimUiButton = new GUIStyle(slimUiGUiSkin.GetStyle("button"));
    13.      }
    14. }
    I put InitStyles(); at the beginning of void OnGUI()

    Thank you!
     
    brownboot67 likes this.