Instantiate

How to create/copy/duplicate an object in Unity?

Instantiate creates/spawns/instantiates a copy/clone of an object.

Examples

Instantiate a prefab when the game starts

The following code will create/spawn/instantiate an instance of a prefab you have referenced.

public class TestScript : MonoBehaviour {

	public GameObject _prefab;

	void Start()
	{
		// Instantiates the prefab into the scene as a GameObject
		Instantiate(_prefab);
		
		// Instantiates with custom position
		Instantiate(_prefab, new Vector3(10f, 20f, 30f));
		
		// Instantiates with custom position and rotation
		Instantiate(_prefab, new Vector3(10f, 20f, 30f), Quaternion.identity);
	}

}

Instantiate with a reference to the instance

Sometimes you want to operate on an object immediately after you have instantiated it.

public class TestScript : MonoBehaviour {

	public GameObject _prefab;
	public GameObject _instanceOfPrefab;

	void Start()
	{
		// Instantiates a GameObject into the scene, but stores the instance in a member variable.
		_instanceOfPrefab = Instantiate(_prefab);
		
		// Now we can do anything with the reference to the instance
		_instanceOfPrefab.name = "Bob";
		_instanceOfPrefab.transform.position = new Vector3(1f, 10f, 1f);
	}

}

Same as above, but stores the instance in a local variable instead of a member variable.

public class TestScript : MonoBehaviour {

	public GameObject _prefab;

	void Start()
	{
		GameObject instanceOfPrefab = Instantiate(_prefab);
		
		instanceOfPrefab.name = "Bob";
		instanceOfPrefab.transform.position = new Vector3(1f, 10f, 1f);
	}

}

Instantiate with a reference to a Component on the instance

Instantiate was updated by Unity to understand what type the object is you are trying to instantiate.

In Visual Studio, you can hover over Instantiate with the mouse cursor and a tooltip will show you the Type it’s trying to return.

public class TestScript : MonoBehaviour {

	// We dragged a prefab in here that contained a HealthScript component
	public HealthScript _healthComponent;

	void Start()
	{
		// Instantiates the full prefab as a GameObject in the scene, then returns the HealthScript component that is on it
		_healthComponent = Instantiate(_healthComponent);
		
		// We can access the HealthScript immediately, without requiring GetComponent.
		_healthComponent.Heal(50);
		
		// Note: To destroy the GameObject, remember to use the gameObject property
		Destroy(_healthComponent.gameObject);
		
		// Destroys only the HealthScript Component on the GameObject, leaving GameObject in the scene
		Destroy(_healthComponent);
	}

}

Before this feature, a hard or soft Cast was required. See the following example:

public class TestScript : MonoBehaviour {

	public GameObject _prefab;

	void Start()
	{
		// Soft cast - will return null if it doesn't work. You can check for null and continue.
		_prefab = Instantiate(_prefab) as GameObject;
		
		// Hard cast - will create an exception if it doesn't work.
		_prefab = (GameObject)Instantiate(_prefab);
	}

}

Notes

  • Instantiate was updated at some point to return the Type of the object you are trying to instantiate, so no casting is required. If using an older version, it is recommened to soft cast to the type you desire, otherwise it will return you an Object.
  • Official Unity Scripting API - Instantiate