Define attacks to choose from
Hi,
i'm looking for a way to create an array or a list to choose a random attack from. The problem is that my attacks are derived classes and i currently don't know how to get the right infos. Since they don't derive Monobehavior i can't create prefabs.
Here is what i got so far:
The base class.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class BaseAttack
{
public string AttackName;
public int AttackID;
public string AttackDescription;
public float AttackDamage;
public float AttackCost;//Mana cost for example
}
and one of my attacks.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class StandartAttack : BaseAttack
{
public StandartAttack()
{
AttackName = "Normal Attack";
AttackID = 1;
AttackDescription = "Just the standart attack";
AttackDamage = 10;
AttackCost = 0;
}
}
I want to make use of differnt attacks, and set them by hand in the editor for each enemy i have to choose any attack from. Like a public List or Array. I hope you understand my problem. What step did i missed?
Answer by UCSIM · Feb 18, 2016 at 10:37 PM
If I understand correctly you are looking to create a list of all attacks that includes any attack that inherits from BaseAttack.
Because StandartAttack inherits from BaseAttack you can create a list of BaseAttacks and then put StandartAttack object in it. As well as any different attack as long as they also inherit from BaseAttack
Example:
List <BaseAttack> attacks = new List<BaseAttack>
And then just add whatever attacks you want to the List.
Basicly you got me right, but id like to drag in the attacks a specific enemy can have into the inspector, in this list. Or how should i make right use of it?
I think i got an idea. If BaseAttack would inherit $$anonymous$$onobehavior i can drag each attack onto an empty game object, create a prefabof it and then drag those into the list. In the end i might have 100 prefabs of different attacks to choose from, and place those into the list of attacks possible to use. Wouldn't i lose the connection to them, once i reload the scene for example?
All my enemys also need to be a prefabs to deter$$anonymous$$e who to fight.
The question now is, is this the only way? Or a good way to do it?
I wouldn't do that, but what you can do is make all of the attacks as scripts. Then you can drag those scripts onto each enemy. After doing that you can add them to a public list like mentioned above that you made on that enemy.
Your answer
Follow this Question
Related Questions
Suggestions for planning project tasks and organizing classes/functionality? 0 Answers
Inheriting values from base class in c# 0 Answers
How do I modify variables from a different class that belongs to the same C# file? 1 Answer
Custom class list seen as "none" in inspector. 1 Answer
List of Class With GameObject 0 Answers