Instantation and use of gameobject in same frame


I would like to know how you deal with a scenario like this:

  1. When a mouse button is clicked a prefab is instantiated.
  2. This prefab have some components that need initialization and the initialization code is placed in awake and start.
  3. Right after creation (same frame) the gameobjects are passed to a function that will do some calculatiosn with it.

You see the problem. The initialization hasn’t been ran for the different components in the game object. So, 3 won’t work or will render a wrong result.

Solutions I have found (thought about):

  1. After creation wait for next frame with a coroutine (WaitForEndOfFrame) and then, execute step 3(pass this objects now that awake and start has been called to the worker function).
  2. Instantiate the game object and them add it to a list. After all frame work is done when LateUpdate() callback of the behaviour that created the gameobject is called, test if there are newly created gameobjects and pass all these newly created objects to the worker function.
  3. Move initialization code to different inits in each behavour and initialize then manually (really bad aproach).

Would you mind commenting any other way to accomplish this?


Just ran a quick test in Unity to confirm. Awake is called before the next line of code, so long as the prefab is an active GameObject.

Here are the scripts and the results.

public class FunctionTestManager : MonoBehaviour {
	public GameObject testPrefab;

	void Start () {
		Debug.Log ("Before Instantiate");
		Instantiate (testPrefab);
		Debug.Log ("After Instantiate");

public class FunctionTest : MonoBehaviour {

	void Awake (){
		Debug.Log ("Awake");

	void Start () {
		Debug.Log ("Start");

Console reads

  • Before Instantiate
  • Awake
  • After Instantiate
  • Start

This type of open-ended question is better suited to the forums.

If you can wait till next frame to use the object and let start/awake handle initialization, I guess that’s fine. I personally prefer custom Inits to all that rigamarole. It really varies case by case which is more appropriate, but I find Inits much cleaner. If you inherit from a base class and override a virtual (blank) Init, you don’t even need to have the script object as its derivative type. I have manual Inits in about a quarter or third of my scripts. They’re not that bad to work with.