In this lesson, you will learn how to decouple your hard-coded configuration values from your scripts by migrating them into external ScriptableObject assets.
The Problem: Hard-Coding
As you build your game, you might find yourself typing values directly into your MonoBehaviour scripts, like public float moveSpeed = 5.0f;. While this works for tiny projects, it quickly becomes a bottleneck. If you have fifty different enemy prefabs and you want to adjust their speed, you shouldn't have to open every script to change the values. Furthermore, your logic (how the character moves) is currently "tangled" with the data (how fast they move).
The Solution: Data Containers
By moving these variables into a ScriptableObject, you create a clean separation. The script becomes a "logic processor" that asks the data asset, "How fast should I go?" rather than "knowing" the speed itself.
1. Create the Data Container:
[CreateAssetMenu]
public class CharacterStats : ScriptableObject {
public float moveSpeed;
public float jumpForce;
}
2. Update your Logic: Instead of hard-coding the variables, your player script now references the container:
public class PlayerMovement : MonoBehaviour {
public CharacterStats stats; // The data lives here!
void Update() {
// Logic uses the data, but doesn't define it
transform.position += Vector3.forward * stats.moveSpeed * Time.deltaTime;
}
}
Why This Matters
When your data is separated, your MonoBehaviour scripts become reusable templates. You can create different CharacterStats assets (e.g., FastRunner.asset, HeavyTank.asset) in your Project window and simply drag them into the PlayerMovement component of any prefab. You can tweak these values in the Inspector while the game is running to balance your gameplay instantly without ever touching your code.
By isolating the "what" (data) from the "how" (logic), you make your code more maintainable, easier to debug, and much faster to balance.
Key Takeaway: By moving hard-coded values into ScriptableObjects, you transform your scripts into flexible logic engines that can be configured via external data assets rather than rigid, manual edits.