[Solved] finding a way to keep instances of multiple classes?

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

like

Private Mage _mage;
Private Warrior _warrior;
Private Thief _thief;
Private Bowman _bowman;

this is what i would like to change so it would be something like

private Class _classes;

You would probably have all of the classes either inherit from the same base class or implement the same interface IPlayable for example.

Then you would have either an array or list of IPlayable’s.

References:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface
https://stackoverflow.com/questions/6802573/c-sharp-interfaces-whats-the-point

1 Like

Here’s my video on Interfaces:

Plug and play is a great option here. Also, you can create a general Singleton manager, “Player Manager” to handle this for you.

Here’s my Singleton Manager video for reference:

i got a feeling that i didn’t write it correctly.

i have lets say 5 classes named mage,thief, etc and all those classes i wish to reference under one varible private class _class;

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.

how would i go about getting the values from the specific class then?

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();
}
}

Does it help a bit more?? :smile:

well i found another way so to speak i created a class thats the parent of all classes, this one will handle updating, etc for the class.

Nice! that is the exact idea i was trying to explain xD some times i confuse ppl more than what i help.

i ended up doing a sort of a component system for my Classes. and i just find out based on what class you are. to update only that specific one.