Editor does not pop up anymore

After Lecture 2-12 [Styling Nodes],
I first received a null reference error for which I performed a check in Dialogue.cs.
Like:

using UnityEngine;
using System.Collections.Generic;

namespace RPG.Dialogue
{
    [CreateAssetMenu(fileName = "New Dialogue", menuName = "Dialogue", order = 0)]
    public class Dialogue : ScriptableObject
    {
        [SerializeField] List<DialogueNode> nodes;
#if UNITY_EDITOR
        private void Awake() 
        {
            if(nodes != null)
            {
                if (nodes.Count == 0)
                {
                    nodes.Add(new DialogueNode());
                }
            }
            
            
        }
#endif
        public IEnumerable<DialogueNode> GetAllNodes()
        {
            return nodes;
        }
    }
}

I don’t have the error anymore, but the editor does not show, no matter where and how I summon it.
I even copied the scripts from repo:

DialogueNode.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RPG.Dialogue
{
    [System.Serializable]
    public class DialogueNode
    {
        public String ID;
        public String text;
        public Rect rect;
        public string[] childrenNodeID;
    }
}

and DialogueEditor.cs

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

namespace RPG.Dialogue.Editor
{
    public class DialogueEditor : EditorWindow
    {
        Dialogue selectedDialogue = null;
        GUIStyle nodeStyle;

        [MenuItem("Window/Dialogue Editor")]
        private static void ShowWindow()
        {
            GetWindow(typeof(DialogueEditor), false, "Dialogue Editor");
        }
        
        [OnOpenAsset(1)]
        public static bool OnAssetOpen(int instanceID, int line)
        {
            Dialogue dialogue = EditorUtility.InstanceIDToObject(instanceID) as Dialogue;
            if(dialogue != null)
            {
                ShowWindow();
                return true;
            }
            return false;
        }

        private void OnEnable()
        {
            Selection.selectionChanged += OnSelectionChanged;
            nodeStyle = new GUIStyle();
            nodeStyle.normal.background = EditorGUIUtility.Load("node0") as Texture2D;
            nodeStyle.normal.textColor = Color.white;
            nodeStyle.padding = new RectOffset(20, 20, 20, 20);
            nodeStyle.border = new RectOffset(12, 12, 12, 12);
        }
        private void OnSelectionChanged()
        {
            Dialogue dialogue = Selection.activeObject as Dialogue;
            if (dialogue != null)
            {
                selectedDialogue = dialogue;
                Repaint();
            }
        }

        private void OnGUI()
        {
            if(selectedDialogue == null)
            {
                EditorGUILayout.LabelField("No Dialogue Selected !");
            }
            else
            {
                foreach(DialogueNode node in selectedDialogue.GetAllNodes())
                {
                    OnGUINode(node);
                }
            }
        }

        private void OnGUINode(DialogueNode node)
        {
            GUILayout.BeginArea(node.rect, nodeStyle);
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.LabelField("Node", EditorStyles.whiteLabel);
            string newID = EditorGUILayout.TextField(node.ID);
            string newText = EditorGUILayout.TextField(node.text);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(selectedDialogue, "Update Dialogue Data");
                node.ID = newID;
                node.text = newText;
            }

            GUILayout.EndArea();
        }
    }   
}

I need some help please…

Let’s start by eliminating the null reference error in Dialogue (technically, also eliminating the need for the null check, but I always encourage null checks.

[SerializeField] List<DialogueNode> nodes = new List<DialogueNode>();

This ensures that nodes will be a fresh new list (not null) when creating the new Scriptable Object.

When you say the Editor does not show, do you mean that no window is created at all, or that the window is simply blank?

If the latter is the case, then the above line should resolve this issue. If the former is the case (no window opens at all), then something has gone wrong that may require going to Unity Tech support.

In the case that nothing is shown in the window… this is because even after your null check, no new nodes were created. In essense, there is nothing for OnGUI to do because there are no nodes. The above change should ensure that there are nodes. There is a chance, however, that any blank Dialogues you’ve already created may have serialized the null state, in which case you’ll need to delete the Dialogue and start with a new one.

Hello Brian!
I did try initializing the list but it won’t do. The editor simoly does not come up. One second it worked, and after setting up the node styling, it just broke.
I guess I will delete these scripts and start over from scratch because I really don’t think I did anything wrong at this point.

Thank you very much for your help!

Did rewriting the scripts do the trick?

Good morning, It did work but I still can’t figure out what I did wrong the first time. It might have to do with another script interfering maybe but then again It was the first one I ever wrote for the editor so…
I guess with more experience I will eventually wise up.
By the way, do you recommend anything to read for getting good with coding in general?

Sometimes, that’s the way of things, you just never know. Practice and experience help you spot what I like to call “error patterns”… things that when you see this happen, you know to look for this. Editor scripting is tricky, because often times, the error messages in an Editor script have no relation to what’s actually happening.

In terms of reading, I don’t have any specific recommendations. I’ve been coding so long that my reading tends to be Googling the specific language feature, function, or class, and getting straight into the Microsoft or Unity documentation.

The biggest factor for success isn’t knowing the syntax, or when to use PascalCase or camelCase, it’s knowing how to take a large problem and break it down into steps. I would look for books on problem solving.

Hello!
Thank you for that insight. I guess there really is nonpoint rushing the learning process if getting good is the milestone in my journey.
I’ll find ways to practice more and take things one step at a time.

Cheers!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms