0
0
Unityframework~10 mins

ScriptableObjects for game data in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ScriptableObjects for game data
Create ScriptableObject class
Create asset instance in Editor
Assign data in asset
Reference asset in game scripts
Use data at runtime
Game behavior based on data
This flow shows how you create a ScriptableObject class, make an asset in Unity Editor, assign data, then use it in game scripts to control behavior.
Execution Sample
Unity
using UnityEngine;

[CreateAssetMenu(menuName = "Game/EnemyData")]
public class EnemyData : ScriptableObject {
    public string enemyName;
    public int health;
}
Defines a ScriptableObject class to hold enemy data like name and health.
Execution Table
StepActionCode/EditorResult/State
1Define ScriptableObject classpublic class EnemyData : ScriptableObject {...}EnemyData type created
2Create asset in EditorRight-click > Create > Game > EnemyDataEnemyData asset file created
3Assign data in assetenemyName = "Goblin", health = 50Asset stores Goblin data
4Reference asset in scriptpublic EnemyData enemyData;Script can access asset data
5Use data at runtimeDebug.Log(enemyData.enemyName);Prints 'Goblin' in console
6Modify asset datahealth = 75Asset data updated, affects all references
7ExitGame runs using asset dataGame uses centralized data for enemies
💡 Execution stops after game uses ScriptableObject data for behavior
Variable Tracker
VariableStartAfter Step 3After Step 6Final
enemyNamenull"Goblin""Goblin""Goblin"
health0507575
enemyDatanullreference to assetreference to assetreference to asset
Key Moments - 3 Insights
Why does changing the ScriptableObject asset affect all game objects using it?
Because all references point to the same asset instance, so changing the asset updates data everywhere (see execution_table step 6).
Can I create ScriptableObject instances at runtime like normal classes?
Usually no; ScriptableObjects are created as assets in Editor to hold shared data (see concept_flow step 2).
How do I access ScriptableObject data in my game scripts?
By creating a public variable referencing the asset and assigning it in the Inspector (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the enemyName value after step 3?
A"Orc"
B"Goblin"
Cnull
D"Dragon"
💡 Hint
Check the 'Result/State' column at step 3 in execution_table.
At which step does the health value change from 50 to 75?
AStep 3
BStep 4
CStep 6
DStep 5
💡 Hint
Look at variable_tracker for health changes and execution_table step 6.
If you create a new ScriptableObject asset, what must you do to use it in a script?
AAssign it to a public variable in the Inspector
BCall new EnemyData() in code
CNothing, it auto-links
DWrite data directly in the script
💡 Hint
See execution_table step 4 about referencing assets in scripts.
Concept Snapshot
ScriptableObjects hold shared game data as assets.
Create a class inheriting ScriptableObject.
Make asset in Editor, assign data.
Reference asset in scripts via public variables.
Changing asset updates all references.
Used for centralized, reusable data.
Full Transcript
ScriptableObjects are special data containers in Unity used to store game data like enemy stats. First, you create a class that inherits from ScriptableObject. Then, in the Unity Editor, you create an asset of this class and fill in the data, such as enemy name and health. In your game scripts, you add a public variable to reference this asset and assign it in the Inspector. At runtime, your game uses this data to control behavior. If you change the asset data, all game objects using it see the update because they share the same asset instance. This approach helps keep data centralized and easy to manage.