Home>

I am currently writing code in Unity C#
When calling an abstract method (function with abstract) in a function of an abstract class
It seems that the method is undefined and an error occurs

How do you describe it when I want to abstract only a specific processing part such as the following?
Is there no choice but to force a default by using virtual for the time being?

abstract class BaseShopRegister{
    protected abstract uint CalcTax(uint cost);
    public void CalcCost(uint cost){
        return CalcTax(cost);// error here.
    }
}
class ShopRegister_Old :BaseShopRegister{
    protected override uint CalcTax(uint cost){
        return cost * 1.05f;
    }
}
class ShopRegister_New :BaseShopRegister{
    protected override uint CalcTax(uint cost){
        return cost * 1.10f;
    }
}

virtual version.

class BaseShopRegister{
    protected virtual uint CalcTax(uint cost){
        return cost;// Force default process? .
    }
    public void CalcCost(uint cost){
        return CalcTax(cost);
    }
}
  • Answer # 1

    Excuse me. I wasn't sure what the error was.
    I was doing something like this using gameObject and it was just null access.

    Calling an abstract method in a function of an abstract class can be done without problems! !! !!

    abstract class BaseShopRegister{
        protected abstract uint CalcTax(uint cost);
        public void CalcCost(uint cost){
            return CalcTax(cost);// error here.
        }
    }
    class ShopRegister_Old :BaseShopRegister{
        public void CalcCost(uint cost){
            base.CalcCost(cost);
            Transform trans;
            trans = transform.Find("CostTextUI");
            Cost_UI_Text = trans.GetComponent<Text>();
        }
        protected override void CalcTax(uint cost){
            Cost_UI_Text.text = cost * 1.05f;// Cost_UI_Text is still null.
        }
        Text Cost_UI_Text;
    }