- Home /
OnObjectCreate event
I need a way to detect when a new gameObject has been created or added to the hierarchy. The intent is to speed up development time for specific gameObjects that will always need the same configuration or component(s).
I tried EditorWindow.OnHierarchyChange() but it seems expensive in that it fires for EVERYTHING, new objects, deleted objects and values changed on objects, and I can't find a way to only parse the changed or new gameObject. I have to make a loop that parses every single object in the scene. Imagine this getting ran on all gameObjects every time a single value is changed.
foreach (Transform x in Transform.FindObjectsOfType<Transform>())
{
// code to parse every gameObject goes here
}
Heres an example of a perfect world
EditorWindow.OnHierarchyNewObject() += ParseNewObject(newObject);
ParseNewObject(GameObject theNewObject)
{
// onetime heavy lifting code goes here and only runs on the new gameObject
}
If you have specific game objects that always need the same configuration or components, why don't you use prefabs?
Because some of the object are fbx files that are created by a 3D artist, these files that include animations would automatically get a GUID component attached.
Answer by AhmedSabry19 · Mar 23, 2016 at 11:01 AM
Why don't you add OnEnable()
method on that object that will be spawned in run-time and write into it whatever you want.
I think that puts me back and square 1, manually adding things when I'm trying to write code to automatically add things.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072920im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get all gameObjects of unloading scene from editor Script 0 Answers
Highlighting / Focusing on an Editor Window Through Code 1 Answer
Where and how are gameobjects stored in files? 0 Answers
Get children of another object in hierarchy. 1 Answer
Is it possible to detect drag and drop in hierachy window? 0 Answers