Abstract and non abstract class
Hi guys,
I am pretty new in this and i got stuck at the begining with my code. I am trying to make my object behaviour and movement but it gives me this error here:
error CS0513: `Creature.Attack()' is abstract but it is declared in the non-abstract class "Creature"
Can you please some one find where my mistake might be on this code below:
using UnityEngine; using System.Collections;
public class Creature : MonoBehaviour {
public string name;
public int health;
public int damage;
public float range;
/// attack spee is one attack per second
public float attackSpeed = 2f;
public void GetHit (int playerDamage)
{
health = health - playerDamage;
}
protected abstract void Attack();
}
Thank you in advance! Nick
Answer by Cepheid · Jan 29, 2016 at 10:20 PM
The error message is telling you the exact problem you've declared an abstract method in a non-abstract class. Abstract methods can only be created within an abstract class. Your class creature is not abstract and thus can not implement an abstract method.
If you want a method that you can override then simply use the virtual keyword which allows you to override the method. If you simply want a method contract for your class then consider using an interface.
But, if you want an abstract method then you'll need to create a separate abstract class with the abstract method Attack() contained within. Then, you will inherit the abstract class and override the Attack() methods implementation within the creature class. I would strongly advise, that if the topic of interfaces, virtual, abstract or overriding confuses you. Then maybe read a bit more about C# and coding concepts before using them. I hope this helps!
Answer by DeadlyPanda01 · Mar 04, 2016 at 10:57 PM
make like that:
using UnityEngine;
using System.Collections;
public abstract class Creature : MonoBehaviour
{
public string name;
bla bla bla;
}