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

Instantiating ScriptableObjects as Project Assets

Implement the CreateAssetMenu attribute to generate and store data containers directly within your Unity project folder structure.

In this lesson, you will learn how to turn your defined ScriptableObject classes into actual, reusable files within your Unity Project window using the CreateAssetMenu attribute.

From Blueprint to Asset

Up to this point, you have learned how to define ScriptableObject classes to serve as data containers. However, a class definition is just a blueprint; it doesn't exist in your project as a piece of data until you "instantiate" it as an asset file (like an .asset file). To make this process seamless, Unity provides the [CreateAssetMenu] attribute.

By adding this attribute above your class declaration, you tell Unity to add an entry to your Right-Click > Create menu in the Project window. This allows you and your design team to create new instances of your data containers just as easily as creating a new Material or C# script.

Implementation

To enable this feature, simply place the attribute above your class definition:

using UnityEngine;

[CreateAssetMenu(fileName = "NewItemData", menuName = "Inventory/Item Data")]
public class ItemData : ScriptableObject
{
    public string itemName;
    public int value;
}
  • fileName: Sets the default name of the file when you first create it.
  • menuName: Defines the path within the Create menu (e.g., right-clicking in the Project window and navigating to Inventory > Item Data).

Once you save this script, go to your Project window, right-click, and look for your custom menu path. Clicking that option creates an .asset file that persists in your folder structure. You can then select this asset and modify its variables in the Inspector without ever touching the code again. This effectively decouples your data from your game objects, allowing you to swap out stats or configurations for different items, enemies, or levels instantly.

Key Takeaway: The [CreateAssetMenu] attribute transforms your data classes into user-friendly assets, allowing you to build a database of modular data files directly within your project hierarchy.