- Home /
Show prefabs instantiated at Start() in the editor?
I have an object being instantiated at Start(). How would I make that instantiated prefab visible in the editor? Do I need to take it out of Startup() and put it in an editor script as PrefabUtility.InstantiatePrefab() instead? All I want are things spawned at startup to be visible in the editor. Is there a way to do that?
do you assign the instanciated object to a public GameObject variable, or just calling the Instanciate method from inside of Start(). and just because you have it assigned to a public GameObject variable then what? is the thing instanciating it a holder/handler/manager, or just the thing that creates it. if you need to say have other things in the scene find the created object then that can take some doing, or just waiting for Find() to return.
void Start () {
_currentState = _doorStartState;
GameObject doorHardwareFrontClone = (GameObject) Instantiate(doorHardwareFront, doorHardware$$anonymous$$ountFront.transform.position, doorHardware$$anonymous$$ountFront.transform.rotation);
doorHardwareFrontClone.transform.parent = doorHardware$$anonymous$$ountFront.transform;
GameObject doorHardwareBackClone = (GameObject) Instantiate(doorHardwareBack, doorHardware$$anonymous$$ountBack.transform.position, doorHardware$$anonymous$$ountBack.transform.rotation);
doorHardwareBackClone.transform.parent = doorHardware$$anonymous$$ountBack.transform;
}
Answer by Tarlius · Feb 26, 2013 at 02:50 AM
I'm pretty sure ExecuteInEditMode is what you're looking for.
The cloning problem you mentioned will be caused by the fact that Start() runs a lot more often in the editor (at least every recompile if memory serves), but the first clone is presumably being saved to the scene (and thus isn't removed when the scene reloads). What you'll probably want to do is keep a reference to object when you make it, so you can destroy it when Start() runs again.
However, its possible the reference will be lost when the script recompiles. Serialising/public'ing the field may avoid the problem ([HideInInspector] will hide it in inspector if you want). If not, you're probably stuck checking the children for a component you know is on the prefab and destroying that game object before re-instantiating (unless you don't care about updating this prefab each rebuild)
Answer by redchurch · Feb 25, 2013 at 09:45 PM
Figured out one solution after a bit more searching. Actually, this seems to result in a collection of cloned objects in the scene.
Your answer