- Home /
Interface on ScriptableObject
Hey! Is there any way I could add a variable to a ScriptableObject that accepts all classes that implement a specific interface?
Example:
My Items are ScriptableObject
Every Item should make something different for "void Use()"
First I make an Interface "IDoInteractions"
Then I make one Class for the Bow and one Class for the Sword that have the method "void Use()" from "IDoInteractions"
I want to attach any of those classes to the corresponding ScriptableObject
Any chance to do this? Or how could I make my character do a different "void Use()" for every Item?
Thank you guys!
Answer by misher · Oct 15, 2019 at 08:16 AM
You can use tons of different approaches here, also using interfaces, the downside of interfaces are that you can't reference them in inspector later. Create a base class for your items, smt like this:
public class BaseItem : ScriptableObject {
public abstract void Use();
}
public class ItemSword : BaseItem {
// implement Use here
}
Try to experiment with different architecture and chose the best fit for you.
Wow Nice idea! Got it to work like this! Awesome :)
I made it like this:
public abstract class IItems : ScriptableObject
{
public string itemname;
public int itemcost;
public virtual void Attack1()
{
Debug.Log("The Item does not override Attack1");
}
}
[CreateAsset$$anonymous$$enu(fileName = "Sword", menuName = "Items/Sword", order = 1)]
public class Sword : IItems
{
public override void Attack1()
{
Debug.Log("I attacked with Sword");
}
}
[CreateAsset$$anonymous$$enu(fileName = "Bow", menuName = "Items/Bow", order = 1)]
public class Bow : IItems
{
public override void Attack1()
{
Debug.Log("I attacked with Bow");
}
}
Finally a solution! I have been trying changes without success. Thank you very much!
Your answer
Follow this Question
Related Questions
storing scripts that implements an interface 0 Answers
[C#] Calling Interface from Array of MonoBehaviour 2 Answers
IDropHandler only getting called when flicking the mouse 0 Answers
How to make a List that can store anything that implements an interface? 4 Answers
All interfaces in one Script 1 Answer