Reputation: 49
Yesterday I've found out about Scriptable Objects in Unity.The first thing that came to my mind is : hey , this is really simillar to inheritance.Let's say I have this:
[CreateAssetMenu(menuName = "Scriptableobjects/enemy_data")]
public class enemydata : ScriptableObject
{
public float HP;
public float mana;
public float damage;
}
If I have three enemies A , B , C i will just create 3 instances of enemy_data in my project assets , and complete HP , mana,damage individually.On each of my enemy monobehaviour i'll say :
public enemydata data;
And I'll drag the instances from project assets in their inspector.This is what I understood about scriptable objects from the tutorials I have seen.But , what if i did this:
public class enemydata
{
public float HP;
public float mana;
public float damage;
}
And just inherit this class?Wouldn't this be the same thing?
public class enemy1:MonoBehaviour , enemydata
{
void print()
{
Debug.Log(this.HP + " " this.mana + " " + this.damage);
}
I know I am missing something so please correct me.Thanks!
Upvotes: 0
Views: 1154
Reputation: 628
You can not inherit from 2 classes and only ScriptableObject assets are draggable in the inspector.
The main use for SO is to easily share the same values/data across different components through simple drag and drop. For example your player have a component that adds/removed hit points from PlayerHealth SO. Then you can have on some completely unrelated place LevelReseter component, you just drag PlayerHealth asset and monitor when it reaches zero to reset the level. It should be noted that ScriptableObjects does not serialize their modified values when closing the game, i.e. they can not be directly used to save data across different app runs.
BTW another use for ScriptableObjects is to achieve Strategy Pattern through inspector drag and drop, i.e. swapping algorithms by swapping ScriptableObject assets implementing the same interface (or even better inheriting from the same base class).
Upvotes: 2
Reputation: 90639
It is exactly the same thing, yes.
BUT ScriptableObject
s are Assets and therefore you can
reference it at multiple places (re-use it)
you can have multiple instances but with different values => easily exchange e.g. settings without having to recompile your code
you can have a base class and inherit different types of ScriptableObjects
and still reference them via the Inspector
This last point can be used to e.g. implement exchangeable behaviour. A bit like an interface but you can exchange the actual method implementation without having to recompile so it can even happen on runtime.
This is a huge advantage against the basic [Serializable] public class EnemyData { ... }
for which you would already in code have to define which type to use exactly.
This makes ScriptableObjects
quite powerful and they have a lot of usecases. (See e.g. How to pass data between scenes in Unity)
Most Unity configurations are based on ScriptableObject
e.g. the AnimatorController
.
Your last example
public class enemy1:MonoBehaviour , enemydata
{
void print()
{
Debug.Log(this.HP + " " this.mana + " " + this.damage);
}
}
makes no sense. You can only derive from one class so either MonoBehaviour
or enemyData
!
If something it would need to be a field like
public class enemy1:MonoBehaviour
{
[SerializeField] private enemydata data;
void print()
{
Debug.Log(data.HP + " " data.mana + " " + data.damage);
}
}
Upvotes: 3