T . Kathiravan
2 min readApr 21, 2021

--

Instantiate an Object in Unity

What is Instantiate ?
It’s a function that clones the original and returns the clone and it makes a copy of the object as we do duplicate in the Unity Editor.
By default the new Object parent is null and it is not the sibling of the original.
The active status of a Gameobject at that time of creation is maintained. If its active the cloned object will be active and vice versa.

Syntax :
Instantiate(prefab, position of the Gameobject,Quaternion.identity);

Position : transform.position - is the gameobject position where it can be instantiated.
Prefabs : Prefabs are special type of components that can be reused at anytime. The prefab acts as a template from which you can create new object instances in the scene.
Quaternion : This quaternion corresponds to “no rotation” — the object is perfectly aligned with the world or parent axes.

so now I will instantiate an object on start

Instantiating Object on Start
on Start()

Right click and create a cube .In the asset panel create Prefab folder and drag the cube to the folder and hence the cube is now an prefab , it can be reused at any time .Now I want to instantiate on Key press -Space key

Create C# script and open it in Visual Studio. Declare the Gameobject which want to instantiate

[SerializeField]

private GameObject _cubePrefab;

and in the update method

if(Input.GetKeyDown(KeyCode.Space))

{

Instantiate(_cubePrefab, transform.position, Quaternion.identity);

}

for every spacebar key press you can see the cube is instantiated. As it uses the same transform position of the empty gameobject , instantiated object overlap, you can move the object from the Unity Editor.

--

--