I have a PokemonBase
ScriptableObject
in Unity that represents a Pokémon with various attributes. I want to automatically set the pokemonName
field within the ScriptableObject
to match the name of the asset when I create a new one.
using UnityEngine;
[CreateAssetMenu(fileName = "NewPokemon", menuName = "Pokemon/Create a new Pokemon")]
public class PokemonBase : ScriptableObject
{
[SerializeField] string pokemonName;
public string PokemonName
{
get{return pokemonName;}
set{pokemonName=value;}
}
// ... other fields ...
}
You can accomplish this using the OnValidate
method.
private void OnValidate()
{
pokemonName = name;
}
OnValidate
gets called when the script is loaded or a value changes in the Inspector.
If you need to update all of the existing scriptable objects names, then I would write an editor window that would load all of the scriptable objects and assign the pokemonName from there. This may not be needed since OnValidate
should run when the script is loaded.