GameDev : How spawn GameObjects?

Matteo Lo Piccolo
4 min readMay 12, 2021

--

After understand how work prefab, now we have to understand what we want to do with the Laser.
The idea is : we press Space Key and at Run Time we instantiate (or SPAWN) an object with own behavoiur right?
So, we spawn a Laser, and the laser go up. Simple behaviour for a Laser (projectile).

First of all, DOCUMENTATION. We ALWAYS look at documentation. We are “problem solver”, but we can’t know EVERYTHING, and often we have to solve problems which we never see before! So, how can we do? Again, documentation.

Unity, honestly, has one of the best documentation and inside it we can find a lot of useful information!

For this specific problem, this is the link to how INSTANTIATE (OR SPAWN) a GameObjects
https://docs.unity3d.com/ScriptReference/Object.html

When we look at this example, the important part is how works the Instantiation, and how to write the syntax.

How works the declaration
How works the syntax

As we can see, we have to write Instantiate() and inside parentheses we have to pass some PARAMETERS, you remember? In this case we can pass an object, an object and a transform, an object and a Vector3, an object, a Vector3 and a Rotation, etc etc

Here we have

a prefab, a Vector3 for a position, and a Rotation.
The explanation of what Quaternion.identity means is here
https://docs.unity3d.com/ScriptReference/Quaternion-identity.html

Now, to translate everything into a code : open our Player script, and write Pseudo Code in the Update methos

First part is easy, and is very similar to the part where we move the Player

If statement to check what button we pressed

More on Input Manager in documentation
https://docs.unity3d.com/ScriptReference/Input.html

This is very similar to the Input Manager we used to control the axes.
The only difference is that check the KEY we press.

When we use Input.
there are three fairly important methods that we will often have to deal with

Input.GetKey
Input.GetKeyDown
Input.GetKeyUp

which return us the key we press, the key we hold on and the key when we release

This is just for information, for now we focus on Input.GetKey
After that we have to open parentheses and pass what key we want to press
In this case, Keycode.Space for the Space Bar

and here we have full documentation on KeyCode
https://docs.unity3d.com/ScriptReference/KeyCode.html

To make a simple test, inside the If statement we pass a simple Debug.Log()

Simple Debug to test

If we press Play

It works!

Now we just have to modify the Debug with our Laser!
To do this, we take the reference, exactly as we have seen in the documentation

We need a faerence to our Prefab

Now in Player’s inspector we can drag and drop the Laser prefab.

REMEMBER, WE NEED A REFERENCE FROM OUR PREFAB, NOT FROM LASER FROM THE SCENE, SO WE DRAG AND DROP THE PREFAB FROM PREFABS FOLDER INTO INSPECTOR

After this, delete the Laser from the scene

Select Laser and click CANC to delete

Little tip : if, for some reason, we change the TYPE of reference, we have to DRAG AND DROP AGAIN from prefabs folder to inspector in this case, because sometimes we have an error because we change, we forgot to drag again, and we have a NULL REFERENCE.

Look at this :
I change TRANSFORM TO GAMEOBJECT REFERENCE

If I back to Unity, in the Player Inspector i have this

TYPE MISMATCH

This is because Unity IS EXPECTING a TRANSFORM, but I have changed to GAMEOBJECT

This is just to know, it’s not the worst problem that can happen to us, but well, it can happen. So we need to be sure to drag back into the inspector before hitting play again.

Now we have a reference to our GameObject, write a code to Instantiate it

We pass the prefab, the position (which is the Player position) and thedefaul rotation with Quaternion.identity.

Press Play and try!

And it works!

Every time we press the SpaceBar we create a Laser at run time! Obviously, this is not the behavior we want right?

In the next article we will clean up the code, clean up the inspector because I made a small mistake and create correct behavior for the laser!

--

--

Matteo Lo Piccolo
Matteo Lo Piccolo

Written by Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

No responses yet