Course contentsShow
← Unity Game Development
Lesson 481 of 8287. Advanced Game ArchitectureFree lesson

Combining Strategy and ScriptableObjects for Data-Driven Design

Use ScriptableObjects to store strategy configurations, enabling designers to tweak gameplay behaviors in the Unity Inspector without writing new code.

In this lesson, you’ll learn how to move strategy logic out of hard-coded classes and into modular ScriptableObject assets, allowing your designers to swap game behaviors instantly within the Inspector.

The Problem: Hard-Coded Strategies

Previously, we implemented the Strategy Pattern by creating specific classes for different behaviors (e.g., AggressiveMovement vs. PassiveMovement). While effective, assigning these strategies often requires manual code changes or complex factory patterns. If your designer wants to tweak the "aggressiveness" of an AI, they shouldn't have to recompile code.

The Solution: Strategy as Data

By turning your Strategy classes into ScriptableObjects, you treat behavior as a "data packet" that the game can load at runtime. You define an abstract base class that inherits from ScriptableObject, then implement concrete assets that live in your Project window.

Consider an AI movement system:

public abstract class MovementStrategy : ScriptableObject 
{
    public abstract void Move(Transform target);
}

[CreateAssetMenu(fileName = "NewMovement", menuName = "Strategies/Movement")]
public class LinearMovement : MovementStrategy 
{
    public float speed = 5f;
    public override void Move(Transform target) => target.Translate(Vector3.forward * speed * Time.deltaTime);
}

Designing in the Inspector

Because LinearMovement inherits from ScriptableObject, you can right-click in your Project folder to create a new "Movement" asset. You can create multiple instances—like FastChase (speed 10) and PatrolSlow (speed 2)—without writing a single extra line of code.

In your AI Controller, you simply hold a reference to the base class:

public class AIController : MonoBehaviour 
{
    public MovementStrategy currentStrategy;

    void Update() 
    {
        currentStrategy.Move(transform);
    }
}

Now, your designers can drag and drop different movement assets into the currentStrategy slot on the AI in the Inspector. They can adjust the speed variable on the asset itself, and the AI will update its behavior immediately. This turns your code into a flexible engine, while the ScriptableObjects act as the "ruleset" that drives the gameplay.

Key Takeaway: By elevating strategies to ScriptableObjects, you decouple logic from implementation, empowering non-programmers to iterate on game feel and complexity using only the Unity Inspector.