Search Unity

Editor Window Null Reference

Discussion in 'Scripting' started by Member123456, Feb 8, 2015.

  1. Member123456

    Member123456

    Joined:
    Oct 17, 2012
    Posts:
    237
    So I am new to working with Editor Windows. I have made one that appeared to work fine, but recently I found out that if I dock it and exit Unity, when I load back up the docked window still shows but it has no contents and it is throwing null reference exceptions.

    Here is code for one of the windows. I'm sure if I get help with this one I should be able to figure out the others.

    Code (CSharp):
    1. using SubsonicGames.EditorUtils;
    2. using SubsonicGames.MiscUtils;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. public class ssEditorTools : EditorWindow
    7. {
    8.     private static ssEditorTools window;
    9.     private float buttonVertSize = 25f;
    10.  
    11.     [MenuItem("SS Tools/Editor Extensions #%y")]
    12.     private static void Init()
    13.     {
    14.         window = (ssEditorTools)GetWindow(typeof(ssEditorTools), false, "SS Editor Tools");
    15.         window.minSize = new Vector2(300f, 300f);
    16.         window.maxSize = new Vector2(300f, 300f);
    17.         window.Show();
    18.     }
    19.  
    20.     private void OnInspectorUpdate()
    21.     {
    22.         Repaint();
    23.     }
    24.  
    25.     private void OnGUI()
    26.     {
    27.         ssEditorGUI.SetSSEditorBG(window);
    28.  
    29.         GUILayout.BeginArea(new Rect(0, 0, window.position.width, window.position.height));
    30.         {
    31.             GUILayout.FlexibleSpace();
    32.             if (ssEditorGUI.CreateCeneteredButton("Setup New Project", "", GUI.skin.GetStyle("Button"), GUILayout.MaxWidth(window.position.width - 25), GUILayout.MinHeight(buttonVertSize)))
    33.             {
    34.                 ssSetupProject.GenerateProject();
    35.             }
    36.             ssEditorGUI.RememberColors();
    37.             if (FindEditorWindow<ssAutoSaveOptions>())
    38.             {
    39.                 ssEditorGUI.SetColor(ssEditorGUI.redLt);
    40.             }
    41.             else
    42.             {
    43.                 ssEditorGUI.SetColor(ssEditorGUI.greenLt);
    44.             }
    45.             if (ssEditorGUI.CreateCeneteredButton("Save Options", "", GUI.skin.GetStyle("Button"), GUILayout.MaxWidth(window.position.width - 25), GUILayout.MinHeight(buttonVertSize)))
    46.             {
    47.                 ssAutoSaveOptions.OpenCloseWindow();
    48.             }
    49.             ssEditorGUI.ResetColor();
    50.             ssEditorGUI.RememberColors();
    51.             if (FindEditorWindow<ssToolbar>())
    52.             {
    53.                 ssEditorGUI.SetColor(ssEditorGUI.redLt);
    54.             }
    55.             else
    56.             {
    57.                 ssEditorGUI.SetColor(ssEditorGUI.greenLt);
    58.             }
    59.             if (ssEditorGUI.CreateCeneteredButton("Quick Bar", "", GUI.skin.GetStyle("Button"), GUILayout.MaxWidth(window.position.width - 25), GUILayout.MinHeight(buttonVertSize)))
    60.             {
    61.                 ssToolbar.OpenCloseWindow();
    62.             }
    63.             ssEditorGUI.ResetColor();
    64.             GUILayout.FlexibleSpace();
    65.         }
    66.         GUILayout.EndArea();
    67.     }
    68.  
    69.     bool FindEditorWindow<TWindowType>() where TWindowType : EditorWindow
    70.     {
    71.         var found = false;
    72.         TWindowType[] windows = Resources.FindObjectsOfTypeAll<TWindowType>();
    73.         if (windows != null && windows.Length > 0)
    74.         {
    75.             found = true;
    76.         }
    77.         return found;
    78.     }
    79. }
    80.  
    The null reference is referencing this piece of code in the ssEditorGUI class

    Code (CSharp):
    1. public static void SetSSEditorBG(EditorWindow window)
    2.         {
    3.             GUI.DrawTexture(new Rect(0, 0, window.position.width, window.position.height), Resources.Load("ssEditorBG") as Texture2D);
    4.         }
     
    Last edited: Feb 8, 2015
  2. Member123456

    Member123456

    Joined:
    Oct 17, 2012
    Posts:
    237
    So I keep digging more and it looks like everything I come across with this error keeps saying you have to have the GUI.DrawTexture in OnGUI(). I do have it in OnGUI() in the main file, so I am confused why it doesn't work. Is OnGUI not activated when Unity opens?
     
  3. Member123456

    Member123456

    Joined:
    Oct 17, 2012
    Posts:
    237
    Well just to close this up, I did eventually find the answer myself. I was not properly storing a reference of the editor window for when Unity shut down, so it basically was null each time unity came back up. I changed the beginning code to this on all the windows, and now it works like a charm.

    Code (CSharp):
    1. private static ssToolbarOptions _instance;
    2.  
    3.     #region INITIALIZATION
    4.  
    5.     public static void Init()
    6.     {
    7.         GetWindow(typeof (ssToolbarOptions), false, "Options");
    8.     }
    9.  
    10.     private void OnEnable()
    11.     {
    12.         _instance = this;
    13.         SceneView.RepaintAll();
    14.     }
    15.  
    16.     public static void OpenCloseWindow()
    17.     {
    18.         if (_instance != null)
    19.         {
    20.             _instance.Close();
    21.         }
    22.         else
    23.         {
    24.             Init();
    25.             _instance.minSize = new Vector2(180f, 200f);
    26.             _instance.maxSize = new Vector2(180f, 200f);
    27.             _instance.Show();
    28.         }
    29.     }
    30.  
    31.     #endregion
     
    elseforty and Lachee1 like this.
  4. elseforty

    elseforty

    Joined:
    Apr 3, 2018
    Posts:
    290
    you saved my life thank you,