typecasting problems, generic list, C#

Hi, I need a generic List to add all my Companion scripts (Archer, Knight, etc).
My Companions derive from a Base Class BaseCompanion( Archer : BaseCompanion).
Then this in code:

public List<BaseCompanion> comps = new List<BaseCompanion>();
public Archer archer = new Archer ();

comps.Add(archer);    //does not  work, why?

Do I need to typecast archer into BaseCompanion? If so, how? I tried implicit, explicit but I cant get it right.
Its important for me to have them in a List for sorting and then access their unique stats. Alternatively I thought about deleting my subclasses and just go with some BaseCompanion instances and set properties via index. Would like to go with first option though if possible.
Thx

This should work as you describe. Maybe the comps.Add line is in the wrong place? Does your code look exactly as you posted, because then you need to move that line into a method.

1 Like

Hi, thanks, I checked everything now and turned out I forgot to save Archer script. It derived from monobehaviour and not BaseCompanion. Silly me :stuck_out_tongue:

No problem!