I’m currently making a pokemon clone and during a battle I want the stage to populate with the enemy and the friendly monsters.
I have a Monster class that has a bunch of predefined methods like ‘attack’ or ‘takeDamage’ and variables like ‘attackStat’ and ‘defenseStat’ :
public abstract class Monster : MonoBehaviour {
//stats
public virtual int attackStat { get { return 0; } protected set { } }
public virtual int defenseStat { get { return 0; } protected set { } }
public virtual void Initialize (string name, int level)
{
print("this should not appear");
}
}
I have specific Pokemon that inherit this class, in order to populate the battle easily with that pokemon. For instance I have a Pikachu class that has a bunch of predefined stats like ‘attackStat’ and ‘defendStat’.
public class Pikachu : Monster {
public override void Initialize (string newName, int startingLevel)
{
print("Initialize method for " + newName + " level: " + startingLevel);
monsterName = newName;
level = startingLevel;
attackStat = 10 + 3 * startingLevel;
defenseStat = 2 * startingLevel;
}
Now here’s where I get into trouble. In a battle I have a script called ‘Spawner’ that Instantiates two pokemon, both pikachu, one enemy and one friendly. Here’s an example of spawning some friendly Monsters:
if (friendlyMonsterPrefabArray[i])
{
GameObject myFriendlyMonsterObject = Instantiate(friendlyMonsterPrefabArray[i], battleCanvas.transform) as GameObject;
myFriendlyMonsterObject.transform.parent = enemyPosition.transform;
myFriendlyMonsterObject.transform.position = new Vector3((enemyPosition.transform.position.x + (i * 5)), enemyPosition.transform.position.y);
myFriendlyMonster = myFriendlyMonsterObject.GetComponent<Monster>();
myFriendlyMonster.Initialize("john", 5);
}
Now the problem occurs that given all this code, for some reason the attackStat and defenseStat are never set. I have them fight each other, and I print out the ‘attackStat’ and it’s always ‘0’. I’m screwing up some sort of inheritance here because I never learned properly how to do it so I kind of botched my way through and just followed compiler errors until there weren’t any more!
Is there a better way to be spawning monsters on a battlefield from a prefab array? Should I be using a constructor? I tried to but it never worked either. I also tried having the Pikachu class change the stats by saying base.attackStat = 5; and it didn’t work either.
Thanks