Question by
Martinpartin · Aug 15, 2018 at 07:18 PM ·
c#editornullreferenceexceptionserialization
Editor nullreference error when playing second time
I'm building a custom node editor that's serializable so its contents persists on play. When I add some nodes, play, stops play and hit play again I get a NullReferenceError in DrawWindow() here;
for (int i = 0; i < connections.Count; i++)
{
connections[i].Draw();
It seems the script iterates through the list and and then throws the error at connections[i].Draw(). Below are parts of my editor script and the connections script.
Editor script:
namespace StoryEditor
{
public class StoryEditor : EditorWindow
{
#region variables
public List<SBaseNode> nodes = new List<SBaseNode>();
public List<Connection> connections = new List<Connection>();
private Vector3 mousePosition;
[SerializeField]
private bool clickedOnNode;
[SerializeField]
private SBaseNode selectedNode;
[SerializeField]
private Rect menuBar;
[SerializeField]
private float menuBarHeight = 20f;
public Connector selectedInPoint;
public Connector selectedOutPoint;
[SerializeField]
private bool didPan;
[SerializeField]
private Vector2 pan = new Vector2();
[SerializeField]
private string savePath;
public enum EditorActions
{
addTriggerNode,
//add other node types..
removeNode,
addConnection,
removeConnection
}
#endregion
#region Init
[MenuItem("Window/Story Editor")]
public static void ShowEditor()
{
GetWindow<StoryEditor>("StoryEditor");
}
#endregion
#region GUI Methods
//window code
void OnGUI()
{
Event e = Event.current;
mousePosition = e.mousePosition;
minSize = new Vector2(800, 600);
DrawMenuBar();
DrawGrid(20, new Color(0.5f, 0.5f, 0.5f, 0.2f));
DrawGrid(100, new Color(0.5f, 0.5f, 0.5f, 0.4f));
DrawConnectionToMouse(e);
UserInput(e);
DrawWindows();
}
//....
void DrawWindows()
{
BeginWindows();
Debug.Log("Connections: " + connections.Count);
for (int i = 0; i < connections.Count; i++)
{
connections[i].Draw();
Debug.Log("Draw iteration " + i + " done");
}
for (int i = 0; i < nodes.Count; i++)
{
nodes[i].nodeRect = GUI.Window(i, nodes[i].nodeRect, DrawNodes, nodes[i].nodeTitle);
nodes[i].DragNode(pan);
//Note: if node calls drawConnector it has to be within window.
nodes[i].inConnector.DrawConnector();
nodes[i].outConnector.DrawConnector();
}
EndWindows();
if (didPan)
{
Repaint();
didPan = false;
}
}
connection script
using UnityEngine;
using System.Collections;
using System;
using UnityEditor;
namespace StoryEditor
{
public class Connection : ScriptableObject
{
public Connector inConnector;
public Connector outConnector;
public Action<Connection> onClickRemoveConnection;
public Color color = new Color();
public Color activeColor = new Color(0, 0.7f, 0, 1);
public void Init (Connector inConnector, Connector outConnector, Action<Connection> onClickRemoveConnection)
{
this.inConnector = inConnector;
this.outConnector = outConnector;
this.onClickRemoveConnection = onClickRemoveConnection;
}
public void Draw()
{
if (outConnector.node.triggered)
{
color = activeColor;
}
else
{
color = Color.white;
}
Handles.DrawBezier(
inConnector.cRect.center,
outConnector.cRect.center,
inConnector.cRect.center + Vector2.left * 50f,
outConnector.cRect.center - Vector2.left * 50f,
color,
null,
2f
);
if (Handles.Button((inConnector.cRect.center + outConnector.cRect.center) * 0.5f, Quaternion.identity, 4, 8, Handles.RectangleCap))
{
if (!Application.isPlaying && onClickRemoveConnection != null)
{
onClickRemoveConnection(this);
}
}
}
}
}
Thanks in advance for any help!
Comment