Hi everyone,
I’ve run into problems where changes to the scene performed by my custom inspectors are not preserved when I close Unity.
The first place where I have this issue is in a custom inspector for a script named “Constellation,” attached to objects with children with “Line” components. I tell the children to find a transform elsewhere in the scene (specifically, find a GameObject representing a star, based on its world position).
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Constellation))]
public class ConstellationEditor : Editor
{
public override void OnInspectorGUI()
{
Constellation constellation = target as Constellation;
if (GUILayout.Button(“Set References”))
{
for (Line line in constellation.GetComponentsInChildren<Line>())
{
// line will find a transform somewhere in the scene and reference it in a Serialized private field.
line.FindReference();
}
}
}
}
I also have a script called “StarField” which contains many GameObjects representing stars. Sometimes, I need to delete and load the stars, and I’ve attempted to implement those functions like so:
// setting up the custom inspector (sorry I got lazy this time)
public void LoadStars()
{
if (File.Exists(Application.dataPath + "/starsData.csv"))
{
string dataString = File.ReadAllText(Application.dataPath + "/Resources/Stars - GameData.csv");
foreach (string line in dataString.Split('\n'))
{
GameObject starObject = Instantiate(starPrefab, transform);
// Do some other things to the star based on csv data.
}
}
}
public void DeleteAllStars()
{
int childCount = transform.childCount;
for (int i = 0; i < childCount; i++)
{
DestroyImmediate(transform.GetChild(childCount - i - 1).gameObject);
}
}
As stated earlier, I’m unable to make lasting changes with these functions. Any help would be appreciated.