Full Unity 2D Game Tutorial 2019 – Scriptable Objects

Sharing is caring!

Full Unity 2D Game Tutorial 2019 – Scriptable Objects

In this section of the tutorial we will dive into Scriptable Objects. Scriptable objects are a way to contain data in asset files that can then be used by your game objects. As an example we could have a car Scriptable object. This could than be used to store all data about a car; speed, gears, image, icon, name and description. We could then use our Scriptable car class to make a mini with 6 gears and a mini with 2 gears and red  paint. For more information check out the manual here.

Full Unity 2D Game Tutorial 2019 – Creating our Scriptable Weapons

We will be using scriptable objects to define our weapons. This will make it easier for us to create and swap weapons for our player. We will start by coding out Scriptable object class.

Code Notes:
We no longer use the MonoBehaviour, instead we use ScriptableObject
The rest of the fields are just the data that each weapon will have.

That’s it, we’ve created out scriptable object class. We can now go into unity and create some weapons. Right click in the project window and go to create. You will see out new scriptable object Weapon.

Scriptable Object Weapon

We can now click Weapon to create a new asset. Remember to keep things neat by placing scripts in the scripts folder and creating a new set of folders for our new scriptable assets.

Scriptable Weapon

I have named my weapon DefaultGun because it will be the default gun for our player and Enemies. Add some settings to your weapon in the inspector and we will proceed to add some code in our player to use these new weapons.

Full Unity 2D Game Tutorial 2019 – Player Code

Since we are now storing the bullet prefab inside our weapon we can remove this old variable in our player. We will then add some code to fire the bullet when we click the mouse and fire towards the current mouse pointer.

Code Notes:
Input.GetMouseDown(0) this will let us know if the user is pressing the left mouse button.
Camera.main.ScreenToWorldPoint converts the position we give it to a position in our world because the screen and world have a different coordinates system.
targetVector is the place we aim at – our position to get the direction we want to bullet to travel.
Bulllet Prefab Changes

One final change before we can test the player shooting is to change the bullet prefab. Open the prefab and set the speed to 25, the mass to 0.2 and linear drag to 0.1. This should let our bullets travel faster. Press play and you should have something similar to this:

Shoot Everything

Full Unity 2D Game Tutorial 2019 – Using Weapon Stats

We have started to use our scriptable object however we are only using the bulletprefab in it at the moment. Let’s update our player some more to allow us to use the other fields.

What have we changed in this code… well we moved the fire code from the update method to its own method for clarity. We have also added a startReload method because we might want to trigger a reload sound in the future. We have added 2 new variable for timing the reload and the shots, 1 for magazine to store how many bullets we have left and a bool to keep track of our reload state.

That’s it for this part of the tutorial. Why don’t you try making some different weapons yourself. The finished source code is hosted on github here.

Previous Part Simple Enemy Ai   –    Next Part User Interface and TextMeshPro

Sharing is caring!

One Reply to “Full Unity 2D Game Tutorial 2019 – Scriptable Objects”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.