hello there unity peeps. i have come across a problem i can’t seem to find a solution for.
my problem is that i have mulitple classes that i wish to instantiate based on what Class your character is. now the thing is each Class also has it own class.
how can i efficently without having to reference all classes and just have one reference which holds the classes and manage them
You need to use inheritance. all of those you mentionend should inherit from another class, lets say “character”. Then you create your variable: private Character[ ] characters. Or private List characters.
I think you should read about inheritance and polimorfism. Please, elaborate on what do u mean with getting the values from the specific class .
In the case of the array you need to know the index it is in, for example iterating the array. In the case of the list you should iterate too. If you dont want to iterate you can use a HashList or a HashMap… the latest works with a key-value system.
well what i mean is if lets say i fetch the Class Mage. now i want all things inside this class mage? how would i go about doing that. cause i assume The List has to be a type of the inherited class?
The class in the List<> should be the father of all classes. If mage,thief, etc are the sons, then Character is the father. So you put the father as the type of the list. Lets say you are iterating trought the List, and you find your object (you need a way to identify it using a key inside the class or maybe implement a comparator class if you want the whole object to be compared), then an example would be:
private List<Character> characters= new List<Character>();
characters.add(new Mage());
characters.add(new Archer());
etc..
For(Character t:characters){
if(t.getCharacterID()==4){
//this is the one i wanted so..
t.castSpell();
}
}