From 3623aa6d71d76cc5dcaf07f4f72e69c64921cc89 Mon Sep 17 00:00:00 2001 From: Team Date: Wed, 24 Nov 2021 01:02:01 +0200 Subject: [PATCH] Fix destroying assets error Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy (UnityEngine.Object) XNode.NodeGraph:Clear () (at Assets/Scripts/xNode-1.8.0/Scripts/NodeGraph.cs:51) XNode.NodeGraph:OnDestroy () (at Assets/Scripts/xNode-1.8.0/Scripts/NodeGraph.cs:83) This can be caused by destroying instantiated NodeGraph. Fixed by instantiating nodes in Awake. --- Scripts/NodeGraph.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index d928f94..ef0a314 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -10,6 +10,15 @@ namespace XNode { /// All nodes in the graph. /// See: [SerializeField] public List nodes = new List(); + + private void Awake() { + List newNodes = new List(nodes.Count); + foreach (var node in nodes) { + newNodes.Add(Instantiate(node)); + } + nodes.Clear(); + nodes = newNodes; + } /// Add a node to the graph by type (convenience method - will call the System.Type version) public T AddNode() where T : Node { @@ -47,7 +56,8 @@ namespace XNode { public virtual void Clear() { if (Application.isPlaying) { for (int i = 0; i < nodes.Count; i++) { - Destroy(nodes[i]); + if(nodes[i]) //Graph destroy nodes only here, but this check prevent nullref when node was destroyed outside + Destroy(nodes[i]); } } nodes.Clear(); @@ -121,4 +131,4 @@ namespace XNode { } #endregion } -} \ No newline at end of file +}