Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

NullReferenceException on 'GetStyle' Suddenly

Discussion in 'Extensions & OnGUI' started by ashulgach, Oct 19, 2018.

  1. ashulgach

    ashulgach

    Joined:
    Aug 8, 2013
    Posts:
    8
    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:
    315
    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.
     
    ashulgach likes this.
  3. ashulgach

    ashulgach

    Joined:
    Aug 8, 2013
    Posts:
    8
    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:
    315
    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.  
     
  5. ashulgach

    ashulgach

    Joined:
    Aug 8, 2013
    Posts:
    8
    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.