- Home /
Objects created in Editor script lose their reference after Play/Stop?
My C# editor class (the target script) declares a public GameObject, which is instantiated in the initial Reset() function via:
folder = new GameObject("My Folder");
But after going in to Play mode and stopping again, folder now has a null reference. It (and its contents) are still in the Hierarchy, but I can't access them without a reference. I'm quite new to C#, coming from C++, so I'm still a bit fuzzy on allocation and persistence.
Is the problem because the script is [ExecuteInEditMode] ?
What do I need to do to keep a reference to 'folder'?
Thanks
AFAI$$anonymous$$ editor scripts aren't executed in edit mode. If you need an update then just attach a delegate to the EditorApplication.update callback. There are others as well and a list of them is here.
Answer by iwaldrop · Nov 22, 2013 at 07:04 AM
This is expected behavior for editor scripts. You should do this:
const string FOLDER_NAME = "My Folder";
private GameObject folder;
void Awake()
{
folder = GameObject.Find(FOLDER_NAME) ?? new GameObject(FOLDER_NAME);
}
Thanks, that's certainly going to be useful for ensuring certain objects exist or are recreated. In this case though, maybe it won't work because by simply re-making the folder I'll lose all the contents ( a bunch of prefabs). So, does this mean the expected method should be to simply rebuild EVERYTHING each time?
You shouldn't lose anything in the GameObject if the scene is saved. But if you do (I can't think of every corner case AT$$anonymous$$), then rebuilding the hierarchy is a trivial matter. By augmenting the above with the following, you can create as many prefabs as you want under your folder whenever it is rebuilt. Just drag those prefabs into the inspector (assu$$anonymous$$g you have one).
public GameObject[] folderContents;
void Awake()
{
bool needsRebuild = folder == null;
// find or create folder here
if (needsRebuild)
{
foreach (GameObject go in folderContents)
{
GameObject instance = GameObject.Instantiate(go, Vector3.zero, Quaternion.identity);
instance.transform.parent = folder.transform;
}
}
}
If you don't have an inspector, then look into Resources.LoadAssetAtPath
EDIT: $$anonymous$$odified code so it should execute.
:) It's always a tricky decision as to how much detail to omit when first asking a question - I left out details I thought were irrelevant, but now I can see are highly relevant. So, first, thanks for a great explanation!, although this isn't a complete solution, at least now I understand what needs to be done.
$$anonymous$$y Editor's functionality is to create lots of procedural geometry, with realtime parametric control, and put it in a folder in the hierarchy. Of course, after entering/leaving Play, the contents of that folder can no longer be parametrically edited, because all the references have been lost. So after reading your explanation, it seems the best solution might be to destroy the originals in the hierarchy, and rebuild everything, thereby re-establishing parametric control over all the objects.