Course contentsShow
← Unity Game Development
Lesson 237 of 8284. Prefabs and Data ManagementFree lesson

Optimizing Memory Usage with Shared Assets

Analyze how multiple GameObjects reference a single ScriptableObject instance to reduce memory footprint and improve runtime performance.

In this lesson, you will learn how to reduce your game's memory footprint by sharing a single ScriptableObject instance across multiple GameObjects.

The Problem of Data Duplication

When you create a new ScriptableObject asset in your project folder, it exists as a single file on your disk. However, if you were to manually duplicate that data or create unique instances for every single enemy or item in your scene, your memory usage would climb rapidly. By design, ScriptableObjects are intended to be "shared assets." When multiple MonoBehaviours reference the exact same ScriptableObject file, they all point to the same location in memory.

Implementing Shared References

Imagine you have an EnemyStats ScriptableObject that defines the health, speed, and damage for a "Goblin" type. Instead of giving every Goblin instance its own unique copy of these stats, you assign the same asset file to the public EnemyStats stats; field on each Goblin's script.

public class EnemyController : MonoBehaviour
{
    // Multiple instances of EnemyController point to 
    // the same memory address for this shared asset.
    public EnemyStats sharedStats;

    void Start()
    {
        Debug.Log("My health is: " + sharedStats.maxHealth);
    }
}

Why This Matters for Performance

Because the GameObjects reference the same asset, Unity only needs to load that data into memory once. If you have 100 Goblins, they are all reading from the same data block.

This approach provides two major performance benefits:

  1. Reduced Memory Footprint: You aren't storing redundant copies of strings, floats, or integers in your RAM.
  2. Global Updates: If you modify a value in the ScriptableObject asset (such as changing the base damage during a playtest), every single Goblin in your scene will instantly reflect that change because they are all observing the same source of truth.

Be cautious, however: since they share the same instance, if you change a value at runtime via a script, it will change for every object referencing that file. Use this for read-only data to keep your memory usage efficient and your game logic predictable.

Key Takeaway: By referencing a single ScriptableObject instance across multiple GameObjects, you eliminate redundant data and ensure your game uses memory as efficiently as possible.