2D Topdown Combat help

Hello,

I am trying to add simple combat to a top-down 2d game I am making, but I am unsure about how to implement such a system. My current attempts leave me with the impression that I am doing something wrong.

My current approach it to create a controller script, and an abstract Ability class, the controller then has references to classes the extend ability. When a key is pressed, the script then calls a use() function overridden in the subclass.

However, I get an error message whenever I try to use ‘new’ to instantiate the subclass of Ability, which I don’t really understand.

I’m guessing this is a bad approach, so my question is, what is a good approach to this?

(The game is going to be similar to Binding of Issac, but with melee weapons, so I’m trying to create a melee weapon right now)

You should post the specifics of the error message here, perhaps along with your code.

Unity does not allow you to instantiate Monobehaviors, which I believe your Ability class must derive from.

In order to create an Ability (or any other Monobehavior), you must use the .AddComponent() function on a game object and that will add it to the game object and return a reference to the Ability.

For your application, what you probably want is another script that controls which ability is called when different keys are pressed, and keeps references to each of them to send them a .use() call.

Kurt