Skip to main content

CollectableData - Scriptable Objects

Written by: Akram Taghavi-Burris | © Copyright 2024
Status

To manage and customize collectables in a game, we use a structured approach with ScriptableObject. This pattern centralizes data, allowing us to create different types of collectables quickly and efficiently without hardcoding values into our scripts. In our system, the CollectableData ScriptableObject handles the key properties of each collectable, including its name, point value, and associated sound.

Why "CollectableData"

Because ScriptableObjects are often used to store data for game elements, a common convention is to add the suffix "Data" to the name of ScriptableObject classes, like CollectableData. This naming pattern makes it immediately clear that the object is primarily a data container rather than a behavior-driven component. By following this convention, it's easier to identify ScriptableObjects at a glance in code, helping to keep your project organized and readable.

Implementation

The CollectableData ScriptableObject is a customizable data asset, designed to hold core information for each collectable item in the game. The main feilds of data in this case are:

  • CollectableName: the name of the colletable
  • PointValue: the value of points to be awarded
  • CollectedSound: the audio clip that plays when the colltable is collected

Although, the values for this data will differ by coolletable, by creating a scriptable object we can

CollectableData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Creation menu for ScriptableObject
[CreateAssetMenu(fileName = "New Collectable", menuName = "Collectable Data")]
public class CollectableData : ScriptableObject
{
public string CollectableName;
public int PointValue;
public AudioClip CollectedSound;

//Force the asset and CollectableName to be the same, using OnValidate()
//OnValidate runs whenever there is a change in the Editor
private void OnValidate()
{
if (string.IsNullOrEmpty(CollectableName))
{//set collectable name to the name of the asset
CollectableName = this.name;
}
else
{//set the aset name ot the colllectable name
this.name = CollectableName;
}//end if
}//end OnValidate()
}
Enforcing Consistency

The OnValidate() method ensures that the CollectableName and asset name stay in sync, maintaining consistent labeling and making it easier to organize assets. Whenever a new collectable is created or modified, Unity automatically calls this method, which checks for any inconsistencies between the asset's name and the CollectableName field.

Creating Collectable Assets in the Editor

With the CollectableData ScriptableObject defined, we can now create specific collectable assets directly in the Unity Editor. This allows level designers to easily add new collectables to the game without modifying code, providing greater flexibility and enabling faster iteration.

Steps to Create a New Collectable Asset

  1. Right-click in the Project Window: In Unity, go to the Project window, then right-click in the asset folder to store your collectables.
  2. Select Create > Collectable Data: In the context menu, go to Create > Collectable Data. Unity will create a new instance of the CollectableData ScriptableObject.
  3. Customize the Properties: Select the new asset, and in the Inspector, customize its properties. You can set values for:
    • CollectableName: Enter a name, such as "Green Apple," "Golden Apple," or "Red Apple."
    • PointValue: Assign a point value, which could vary depending on the collectable's rarity or importance.
    • CollectedSound: Choose an audio clip that will play when the collectable is picked up.

Example Assets

For our game, we could create the following collectable assets:

  • Red Apple: A standard collectable with a unique sound.
  • Golden Apple: Rare collectable with a high point value.
  • Green Apple: A hazardous collectable with a negative point value.

By using ScriptableObjects to create these assets, we empower level designers to mix and match collectables as needed, creating more diverse gameplay scenarios without requiring additional development time. Each collectable asset can be customized individually, making it easy to adjust point values, sounds, or other properties as the game evolves.

Data Assets to Gameplay Objects

Now that we have created specific collectable data assets for items like our Green, Golden, and Red Apples, it's important to remember that these ScriptableObjects are purely data containers. While they hold the essential information needed to define each collectable, they don't actively function within the game world.

To bring these assets to life, we need to create in-game objects that can read and apply this data. This is where the Collectable Component comes in. By adding a component that references our CollectableData assets, we can link the data with actual objects in the game world, allowing them to respond to player interactions, update the score, and trigger sounds upon collection.

In the next section, we'll dive into setting up the Collectable Component to bring our collectables to life and make them interactive within the game world.