Hey there, I have a question to ask. Here is sample code for a base class called: StatAttribute and another class that inherits from it. Code (CSharp): public abstract class StatAttribute { public StatAttribute(GameObject character) { this.character = character; } protected GameObject character; protected int baseValue; public int BaseValue { get => baseValue; set => baseValue = value; } public abstract int BonusValue(); public int FinalValue() { return BaseValue + BonusValue(); } } Code (CSharp): using UnityEngine; using System.Linq; public class Strength : StatAttribute { public Strength(GameObject character) : base(character) { } public override int BonusValue() { IStrengthBonus[] bonuses = character.GetComponents<IStrengthBonus>(); if(bonuses.Length > 0) { return bonuses.Sum(bonus => bonus.GetBonus()); } else { return 0; } } } Question: Is it possible to pass in an "interface" object/type?? (ex: IStrengthBonus) into the constructor of StatAttribute so I can move the BonusValue() function to the base class itself?
Maybe something like this? You can use generics, as long as it fits GetComponent constraints. Code (CSharp): public interface ISomeInterface { } public abstract class BaseClass { public abstract void DoSomething<T>() where T : MonoBehaviour; // Or any other constraint, like IStrengthBonus } public class Inheritor : BaseClass { public override void DoSomething<T>() { GameObject go = new GameObject(); // For testing purposes only T[] components = go.GetComponents<T>(); } } Also, calling classes XAttribute might not be a good idea. That naming usually used by C# attributes, e.g. it will be suggested as [Stat].
Make your base class generic and pass that interface as a type argument: Code (CSharp): public class Stat<T> { } public class Strength : Stat<IStrengthBonus> { }