- Home /
How to create a repository of enemy objects that are initialised but then get a new instance of a random one?
I am attempting to create a really simple turn based rpg. I have created an enemy object as follows:
public class Enemy()
{
int attack;
int hp;
string name;
public Enemy(string _name, int _hp, int _attack)
{
name = _name;
hp = _hp;
attack = _attack;
}
}
I then have a separate class where I create specific instances to represent possible enemies to encounter in the game as follows:
public static class Enemies
{
static Enemy slime = new Creature ("Slime", 12, 2, 0, 2);
static Enemy wolf = new Creature ("Wolf", 40, 3, 2, 5);
public static List<Enemy > all = new List<Enemy > {
slime,
wolf,
};
public static Enemy Get (List<Enemy > enemyList)
{
return enemyList[Random.Range (0, enemyList.Count)];
}
}
However if I use this method, the Get method returns that specific instance of the enemy, when I would like for it to return a new instance (so that hp is full etc.).
I have a feeling there is a much better way of doing this, could anyone point me in the correct direction?
Many thanks,
Robin
Answer by bogs101 · Jan 24, 2018 at 07:41 AM
Hi Niboruga,
you're having the same problem i am currently having.
Did you happen to find a solution?
I stole this code from https://youtu.be/Dke0dlCgFSk and modified it for my own purposes.
Problem is that it uses a static dictionary. I assume this means that i won’t be able to create more than one instance of it. I however would like to create instances of this dictionary. if i remove the static then i get an error "an object reference is required for the non-static field, method or property...."
Your answer
Follow this Question
Related Questions
How to choose base class to make Inherit C# 2 Answers
Trying to track the BoxColliders of 2 Different Sets of Instantiated GameObjects 0 Answers
Problem with passing object to another script. 1 Answer
can someone tell me whats wrong with this code? 1 Answer
Unity Serialization with XML Root problem: 'Does Not Denote Valid Type' 0 Answers