Hi all, I’ve got a system I am working on that creates an array of nodes on the map. So far its working pretty well for what I need, but the only problem is that it can’t save the array when I save the scene. The array gets generated by a button in the editor, but it doesn’t seem like its changing the scene so I can’t save it. Any idaes why this is happening?
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class NodeGenerator : MonoBehaviour
{
public int xSize = 50;
public int zSize = 50;
public Terrain terrain;
public GameObject nodePrefab;
private GameObject[,] nodeArray;
void GenerateNodes()
{
if (nodeArray != null)
{
DeleteNodes();
}
nodeArray = new GameObject[xSize, zSize];
for (int x = 0; x < xSize; x++)
{
for (int z = 0; z < zSize; z++)
{
float newX = transform.position.x + x + 0.5f;
float newZ = transform.position.z + z + 0.5f;
float terrainHeight = terrain.SampleHeight(new Vector3(newX, 0, newZ));
nodeArray[x, z] = Instantiate(nodePrefab);
nodeArray[x, z].transform.SetParent(transform);
nodeArray[x, z].transform.position = new Vector3(newX, terrainHeight, newZ);
nodeArray[x, z].name = ("Node: " + x + ", " + z);
}
}
}
void DeleteNodes()
{
if (nodeArray == null)
{
Debug.Log("No nodes to delete.");
return;
}
for (int x = 0; x < xSize; x++)
{
for (int z = 0; z < zSize; z++)
{
GameObject node = nodeArray[x, z];
if (node != null)
{
DestroyImmediate(node);
}
}
}
nodeArray = null;
}
#if UNITY_EDITOR
[CustomEditor(typeof(NodeGenerator))]
public class NodeGeneratorEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
NodeGenerator nodeGenerator = (NodeGenerator)target;
GUILayout.BeginHorizontal();
if (GUILayout.Button("Generate Nodes"))
{
nodeGenerator.GenerateNodes();
}
if (GUILayout.Button("Delete Nodes"))
{
nodeGenerator.DeleteNodes();
}
GUILayout.EndHorizontal();
}
}
#endif
}
I can get around this save problem by doing something like just adding a cube, then save the scene. But would prefer to be able to save after adding the nodes.