Array of unique inherited classes?
Hi, I am trying to make a game where the player can change between many unique attacks, so I was planning on making a base class "Attack" with a method "activate" for when it is called, then in my player combat script I would have an array of classes that inherit from Attack, each one with different code inside the activate method, but I can't figure out how to actually do it.
Here is an example of what I was doing:
public class Player : TurnType {
public List<AttackTypes> attacks = new List<AttackTypes>();
private AttackTypes currentAttack;
void Awake(){
currentAttack = attacks [0];
}
public override bool Attack (){
if (Input.GetButtonDown ("attack")) {
base.Attack ();
print (attacks);
currentAttack.Activate ();
return true;
}else {
return false;
}
}
}
[System.Serializable]
public class AttackTypes : MonoBehaviour {
void Awake(){
GameGlobal.player.GetComponent<Player> ().attacks.Add (this);
}
public virtual void Activate(){
}
}
[System.Serializable]
public class fireball:AttackTypes {
public override void Activate(){
base.Activate ();
// code for attacking
}
}
So what is the issue? This should work, what i don't get is why you need to have the AttackType class inherit from $$anonymous$$onoBehaviour do you need to add it as a Component to a GameObject, or more precisely do you need the Awake Start etc functions on each attack type?
Thank you for the reply.
I don't know what the problem is, other than I know that the Awake() function is never called, so I think it might be something to do with the class never having an instance of it being made? I'm fairly new to program$$anonymous$$g and I only have the most basic understanding of the concepts.
I have it inherit from $$anonymous$$onoBehavior because I need the Start and Awake functions, it doesn't need to be attached to a gameobject.
As long they are not attached to any GameObject these functions won't get invoked thus either you keep them as $$anonymous$$onos and attach them to a GameObject else remove it and handle it internally. Ex.
void Awake()
{
attacks.Add(new fireball());
currentAttack = attacks[0];
}
And of course you need to call your Attack() method from somewhere which is not included on above scripts.
Note1: please use comments to reply ins$$anonymous$$d of answer.
Thank you for your help, that works.
Sorry about the wrong format on the reply, I'm still trying to get used to the website.
Your answer
Follow this Question
Related Questions
(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers
Inheritance and Update Method 1 Answer
C# - creating a list of classes 5 Answers
Help with object oriented 0 Answers
Using a subclass in the inspector in place of a parent class and accessing the subclass variables 1 Answer