Using interfaces and Monobehaviour behaviours
Hi there, I've been trying to utilise interfaces in order to create clean code. I have an interface, IAttackBehaviour that has one void of Attack(), which a character has reference to to invoke it's attack function. Like:
IAttackBehaviour atkBehaviour
void Start()
{
atkBehaviour = new RockThrow()
}
void Update()
{
if(input.GetKeyDown("space"))
{
atkBehaviour.Attack();
}
}
Where one IAttackBehaviour subclass is a RockThrow class that will instantiate a rock projectile as it's Attack() function, but because interfaces don't inherit monobehaviour, I'm unable to do this directly from the RockThrow class, I was wondering what a good solution would be?
Thank you.
Why wouldn't you be able to instantiate an object in the RockThrow
class ?
Because I'm creating a new RockThrow() as my AttackBehaviour I can't create a new $$anonymous$$onoBehaviour as a result, so I don't have access to Instantiate to create the rock projectile
Instantiate
is available in any classes you create in Unity. You can use Resources.Load
to get a reference to a prefab you want to instantiate.
Your answer
Follow this Question
Related Questions
Case of the Haunted Standard Car Assets? 0 Answers
Changing the UI Text 0 Answers
How to change/fix Shader Graph node connection ports 3 Answers
Unity Monodevelop Error - All Scripts are Broken 0 Answers
Private fields getting serialized 0 Answers