0
0
Unityframework~30 mins

ScriptableObjects for game data in Unity - Mini Project: Build & Apply

Choose your learning style9 modes available
ScriptableObjects for game data
📖 Scenario: You are creating a simple game where you want to store data about different weapons. Instead of hardcoding the data, you will use Unity's ScriptableObjects to keep the weapon data separate and easy to manage.
🎯 Goal: Build a ScriptableObject in Unity to hold weapon data like name, damage, and range. Then create a script to use this data in the game.
📋 What You'll Learn
Create a ScriptableObject class called WeaponData with fields for weaponName (string), damage (int), and range (float).
Create an instance of WeaponData in the Unity Editor.
Create a MonoBehaviour script called WeaponUser that has a public WeaponData variable.
In WeaponUser, write code to print the weapon's name, damage, and range to the console.
💡 Why This Matters
🌍 Real World
ScriptableObjects help organize and reuse game data easily without hardcoding values. This makes your game easier to manage and extend.
💼 Career
Game developers use ScriptableObjects to separate data from logic, improving workflow and collaboration between designers and programmers.
Progress0 / 4 steps
1
Create the WeaponData ScriptableObject class
Create a public class called WeaponData that inherits from ScriptableObject. Add public fields: string weaponName, int damage, and float range. Add the [CreateAssetMenu] attribute with fileName = "NewWeaponData" and menuName = "Game Data/Weapon Data".
Unity
Need a hint?

Remember to use ScriptableObject as the base class and add the CreateAssetMenu attribute to make it easy to create instances in the editor.

2
Create a WeaponData asset in the Unity Editor
In the Unity Editor, create a new WeaponData asset by right-clicking in the Project window, then selecting Game Data > Weapon Data. Name it BasicSword and set weaponName to "Basic Sword", damage to 10, and range to 1.5.
Unity
Need a hint?

This step is done in the Unity Editor, not in code. Make sure you create the asset and set the fields exactly as instructed.

3
Create WeaponUser script to use WeaponData
Create a public class called WeaponUser that inherits from MonoBehaviour. Add a public field WeaponData weaponData. In the Start() method, write code to print the weapon's name, damage, and range using Debug.Log.
Unity
Need a hint?

Use Debug.Log inside the Start() method to print the weapon data fields.

4
Assign WeaponData asset and test output
Attach the WeaponUser script to a GameObject in the scene. Assign the BasicSword WeaponData asset to the weaponData field in the Inspector. Run the game and observe the console output.
Unity
Need a hint?

Make sure you assign the ScriptableObject asset to the script in the Inspector before running the game.