‘Instantiating GameObjects in Unity — what, when, where’

Zach Lo
2 min readApr 29, 2021
Photo by Milad B. Fakurian on Unsplash

So now we’re slowly getting the hang of Unity. Bringing in GameObjects, moving them around, changing their material, but how about creating objects and having them appear in our scene through code? In what…instantiating? insta wha? Before instantiating an object we need to make sure we do a few things. First, we need to know what GameObject we are instantiating. To do this we need to create a reference to the object we want to instantiate in our class (Fig 1).

Fig 1

Second, we need to drag our GameObject from our project view to the reference in the player script within the Unity editor (Fig 2).

Fig 2

From here we can instantiate or create the object through code by typing the following (Fig 3 & 4)…

Fig 3
Fig 4

Within the parenthesis we need to include the object we are creating, the position, and if we are adding rotation. For most use cases, we will not be adding rotation so in code we use Quaternion.identity which means in a nutshell no rotation.

--

--

Zach Lo

Unity Developer | Community Manager with a passion for tech and entertainment