I have nodes in my game, that have a List filled with connected nodes. I don’t want to manually add nodes to each other with drag and drop, but I want to connect all nodes that are currently selected in the editor. I wrote this script:
[MenuItem("Node Tools/Connect Selected Nodes %g")]
static void ConnectSelectedNodes() {
List<AbstractNode> selectedNodes = new List<AbstractNode>();
foreach (GameObject n in Selection.gameObjects) {
if (n.GetComponent<AbstractNode>() != null) {
selectedNodes.Add(n.GetComponent<AbstractNode>());
}
}
Debug.Log(selectedNodes.Count);
foreach (AbstractNode n in selectedNodes) {
foreach (AbstractNode c in selectedNodes) {
if (n.gameObject != c.gameObject) {
Undo.RecordObject(n, "Added connection");
n.AddConnection(c);
}
}
}
}
The connection gets added in the inspector, but when I press play, every field in the array switches to “null” and stays like that after stopping the game.
I hope you guys can help me with this problem.