Back to Top

Home ยป Tutorials

Unity Game Programming Tutorials

Working with instantiate, quaternions and random

Hello again! We have used predefined entities in our projects so far, but wouldn't it be nice to be able to create them at runtime? This way, if we created a brick breaker game, for example, we wouldn't need to place dozens of bricks in each level manually, because we can use the C# "for" instruction for that. By the way, if you want to learn pure C# programming, Tutorials Teacher is a great site, which allows people to create and run their code without needing to install an IDE.

To get started, create a new 2D project, give it a relevant name, and the click the blue "Create" button. Wait for a minute or so, until Unity sets things up for you. If everything is okay, you will see a "SampleScene" which uses a "Main Camera" in the Hierarchy view.

Let's create a new script! Right click "Assets" in the Project view, choose "Create", and then "C# Script". Give the resource a relevant name; I have chosen "RuntimeEntities". Then, double click the C# file to open it inside Visual Studio.

runtime entities

We don't need the Start() method for this tutorial, so we will delete its code. Then, let's add a few lines to our RuntimeEntities class; I have highlighted the new additions with red.



private GameObject Medal;

As you know, SerializeField will display the properties of "Medal" in the Inspector. This GameObject doesn't exist yet, but we will create it and assign it the "Medal" name from within the Unity editor.

Let's examine the code inside the Update() method:

if (Input.GetKeyDown(KeyCode.Space))


    Instantiate(Medal,, Quaternion.Euler(0, 0, 0));


The "Instantiate" line is executed if the player presses the space key on his/her keyboard. When this happens, the medal prefab is created at a position given by, which is just a fancy convention for (X = 0, Y = 0, Z = 0). The medal will have an orientation that's given by Quaternion.Euler, allowing use to set individual angles for the X, Y and Z axis. Don't worry, we'll get into that soon, after we get this project started.

Don't forget to save the code in Visual Studio, and then go to Unity. We need to create a "Medal" prefab, and then assign it our script file. Begin by right clicking "Assets" in the Project view, and then choose "Import New Asset".

import asset

You can import any of the files which are supported by the Unity engine, be them 3D models, sprites, etc. I chose to import a flat_medal1 image which has been saved as a png file.

Drag the imported sprite from the Project view in the Hierarchy view, and then drag it back to the Project view to create a prefab out of it. Right click the prefab, choose "Rename", and then set its name to "Medal".


We can now delete the sprite from the Hierarchy view; we won't need it from now on.

Right click an empty area in the Hierarchy view, and then choose "Create Empty" to create an empty object; by default, its name will be set to "GameObject".

create empty

Drag the "RuntimeEntities" script from the Project view over our "GameObject" in the Hierarchy view; this will attach our script to the empty game object. Take a look at the Inspector on the right side of the screen; a new "Runtime Entities" section has now been added to the object, and since we've used SerializeField in our code, we can now tell the engine which object (prefab) is our Medal.

Drag the Medal prefab in its corresponding field, the way I've done it in the picture below.

medal prefab

Phew! It took us quite a bit of time, but we are ready to run the project. Do that, and then press the "Space" key to instantiate a medal sprite.


There's a small problem, though; our sprite is created at X = 0, Y = 0, Z = 0 every single time! So, let's modify the code, making it create randomly placed medals which use randomly generated angles. Here's the modified version of the script.

modified script

The good news is that only these two lines of code have been modified:

Vector3 medalPosition = new Vector3(Random.Range(-10.0f, 10.0f), Random.Range(-5.0f, 5.0f), Random.Range(-10.0f, 10.0f));

Instantiate(Medal, medalPosition, Quaternion.Euler(0, 0, Random.Range(0, 360)));

The first line creates a vector with three components which is named medalPosition. Its X component is set to a random value that ranges from -10 to 10, its Y component is set to -5...5 and its Z position is set to -10...10. I chose a smaller range on the Y axis because my monitor (and probably yours as well) has a bigger width in relation to its height.

The second line creates a Medal sprite anytime we hit the space key, just like before. However, the sprite will be instantiated at the random position given by medalPosition, using a random angle which has been set by Quaternion.Euler. I could have used random values for all the Quaternion components, but trust me: changing the Z (roll) angle is more than enough. Well, if you don't trust me, you can replace the first two zeros with Random.Range(0, 360).

That's all for today, guys! I'll let you enjoy the end result of our hard work. And I'm not talking just about the hard work we had to do by running the project, and then pushing the space key a few hundreds of times...