Day 9 — Let’s get instantiating in Unity

Hey and welcome!

We’re going to be learning about how to instantiate an object or in other words the act of spawning something into our game from thin air.

Instantiate(GameObject, Position, Rotation);

It’s important to note however that only prefab objects can be instantiated. A prefab is simply a shared instance of a single object, so in relation to this project we’re going to need a laser object that the player can fire continuously at the enemy so it makes sense that we’ll want multiple instances of the one object that all share the same components and properties.

Setting up prefabs in Unity is super easy as well and is one of my favourite aspects in Unity:

  • Create a 3D capsule object and scale down it’s size to 0.3 on the x, y and z
  • Next create a Prefabs folder in your assets folder in the project window
  • Then drag your newly created 3D object into the new folder

And that’s it! Now the fun part here is that you can delete your capsule object in the hierarchy without worrying about losing it because you’ll always have an instance of it available to you in your prefabs folder that you can drag back into your hierarchy whenever you want a closer look.

It’s important to talk about overrides as well when first learning about prefabs. Since prefabs are the shared instance of the same object you can add components and scripts onto the object in the prefab folder and those added components will show up if you have your prefab object in the hierarchy at the same time.

If you add these components to your prefab object in the hierarchy it won’t be applied automatically to your object in the prefab folder which is where overrides come in. You can apply an override that will add any changes you’ve made to the object in the hierarchy to your object in the prefabs folder.

Let’s see this in action, first let’s add a Rigidbody component to our capsule in the hierarchy using the add component option in the bottom of the inspector window when clicking on our capsule. Make sure not to choose the 2D one since we’re working with a 3D object here.

Now to apply the override click the dropdown window for overrides near the top of the inspector and choose the apply all option.

Instantiate the laser

if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(_laser, transform.position + new Vector3(0.5f, 1f, 0), Quaternion.identity);
}

Although we’re going to need to make sure that our script has a reference to the laser game object so let’s define a new GameObject variable.

[SerializeField]
private GameObject _laser;

That serialize field attribute is important as it will let us do this:

Now our script has a reference to the laser prefab, remember that it’s the prefab specifically we need otherwise we won’t be able to intsantiate it. With this you should be getting a new laser object appearing everytime the space key is pressed as Input.GetKeyDown(KeyCode.Space) is the condition we’re checking which becomes true from it.

Instantiate(GameObject, Position, Rotation);

We have all the necessary fields filled out here too, our GameObject is our _laser variable defined earlier. The Position is working off of the players current position using the trusted transform.position we’re familar with plus a new Vector3 is added to offset the position by 0.5f on the x and 1f on the y relative to our players position. Lastly our Rotation is being defined as Quaternion.identity which simply means that it’s the default rotation.

That’s all from me with this one, we’ll be sorting out the behaviour of the laser in the next one but for the time being you should have some highly ineffective lasers spawning: