- Home /
Changing the value of a ScriptableObject field with [ExecuteInEditMode] keeps the prefab link to the field
Hi! I'm trying to make a simple tool to change the type of enemy I put in a scene. Changing the type should change its sprite, its name and its base stats which are in a scriptable object. The enemy is a prefab and by changing those value I can “transform” it to the specific enemy I want.
I created a script which runs in edit mode:
[ExecuteInEditMode]
public class EnemyChooser : MonoBehaviour
{
public EnemyType enemyType;
public List<CharacterStats> enemies; // Scriptable objects containing the specific basic stats of each enemies type.
private EnemyCharacter _character;
void Awake()
{
_character = GetComponent<EnemyCharacter>();
}
void Update()
{
if (Application.isPlaying)
return;
if (enemyType == EnemyType.None)
{
_character.baseStats = null;
_character.characterSprite.sprite = null;
gameObject.name = "EnemyCharacter";
return;
}
if (_character.characterSprite.sprite != enemies[(int)enemyType-1].characterSprite)
{
_character.baseStats = enemies[(int)enemyType-1];
_character.characterSprite.sprite = enemies[(int)enemyType-1].characterSprite;
gameObject.name = "EnemyCharacter_" + enemyType.ToString();
}
}
}
public enum EnemyType
{
None,
Sucubus,
Triton
}
With that, when I select an EnemyType in the inspector, it changes the sprite, the name and the scriptable object. That’s fine.
However, the field of the scriptable object doesn’t become bold, meaning it is still linked to the prefab value. When I hit Play, it reverts to the prefab default value. How can I tell the field to become bold?
It only happens this the scriptable object field. The sprite field becomes bold. Also, if I manually change the value of the scriptable object, it also become bold.
Can someone help me please? Thank you!