Loading...
Loading...
Architect Unity 6 data and decoupling with ScriptableObjects: config/data assets, shared runtime variables, event channels, and runtime sets/registries. Use when designing data-driven systems, replacing singletons/managers, creating .asset data with CreateAssetMenu, or when the user mentions ScriptableObject, SO architecture, or data assets.
npx skill4agent add gamedev-skills/awesome-gamedev-agent-skills unity-scriptableobjectsScriptableObjectstatic*.asset: ScriptableObjectsave-systems[System.Serializable]ScriptableObject[CreateAssetMenu].asset[SerializeField]OnEnableusing UnityEngine;
[CreateAssetMenu(fileName = "WeaponData", menuName = "Game/Weapon Data", order = 0)]
public class WeaponData : ScriptableObject
{
public string displayName = "Pistol";
public int damage = 10;
public float fireRate = 0.25f;
public GameObject projectilePrefab;
}public class Weapon : MonoBehaviour
{
[SerializeField] private WeaponData data; // assign the shared asset in the Inspector
private void Fire() => Debug.Log($"{data.displayName} for {data.damage}");
}[CreateAssetMenu(menuName = "Game/Float Variable")]
public class FloatVariable : ScriptableObject
{
[SerializeField] private float initialValue;
[System.NonSerialized] public float runtimeValue; // not saved to the asset
private void OnEnable() => runtimeValue = initialValue; // reset each play session
}
// Player writes playerHealth.runtimeValue; the HUD reads it — neither references the other.// For transient SO data you build in code (e.g. a generated config).
var temp = ScriptableObject.CreateInstance<WeaponData>();
temp.damage = 25;
// ...use temp... Destroy(temp); // clean up runtime-created instances[NonSerialized]OnEnableOnEnableOnEnableruntimeValueISerializationCallbackReceiverOnEnableOnEnableOnDisableOnDestroyUpdatesave-systemsCreateInstanceDestroyGameEventreferences/event-channels.md/Manual/class-ScriptableObject.htmlScriptReference/ScriptableObjectScriptReference/CreateAssetMenuAttributeunity-csharp-scriptingsave-systemscard-gamerpgsurvival-crafting