In Unity, is it possible to automatically set a value when a ScriptableObject is created?
In the following example, I have a parent class, PickupData, and a child class, MoneyPickupData. Whenever an SO of the child class is created (via right-click in the Editor), I would like it to automatically set its type to EPickupType.Money.
I have tried using the Awake method, and I have also tried using the OnEnable method.
What I find is that when the SO is created, the value will be set to Money for a moment while I type in the name of the SO. But as soon as I press Enter and the SO finishes creating itself, the value gets reset to the first value in the enum.
How can I make it so that the value gets set to Money and does not reset?
Parent class
public class PickupData : ScriptableObject
{
// The type of this pickup
public EPickupType type;
}
Child class
public class MoneyPickupData : PickupData
{
public void Awake()
{
// Set the type of this pickup
type = EPickupType.Money;
}
}
The answer was sort of included in my question. The problem was the resetting. So I checked the Unity docs and found the
Resetmethod:This means that the change I make in
Awakegets overridden by this automatic call toReset.The solution for me was to change
AwaketoReset.