- Home /
OOP with specialized classes as subclasses
Hello, I am making a Unity project that is quite simple, but it will get reused a lot of times with small adjustments.
It is not a traditional game and therefore difficult to explain without breaking an NDA. I am trying to restructure my code into more reusable, better code and I was wondering to what extent subclasses are useful.
Example 1: I have a script X that takes in a string message and I want it to call X functions in the same script. But in a similar project/scene I want the same script A to do X+ 3 more functions. Currently I am using bools to enable and disable behaviour, but I was wondering if making subclasses is better, both performance-wise and readability-wise, as it is unecessary to check the bool every frame, when it will constant during the whole scene. Would it be good practice to make a subscript (subclass) B that inherits from script A and just overrides the function? I am not sure what is the best practice to do this, even with subclassing. Do I override/overload the function or do I need to completely rewrite the function?
Example 2: I have a rotating object. In some scenes/projects I want it to snap into place, sometimes I want it to interpolate. I have now made a bool that enables/disables interpolating, would it be better practice to make a subclass that is called script XInterpolation and put the extra behaviour there? If so, how do I just "add" to a function. AKA, I want a function from the base class to run, but also additional behaviour. Is this through overriding?
It is not behaviour that will change during runtime. It will be build-specific.
Also, I have read that Start() is just called in the lowest-level subclass, but upon testing this with a simple script I got that both Start() functions were called. Is this an "unstable" behaviour? Should I assume that only the lowest-level Start() will get called? This is not with virtual void Start(), but void Start() in both baseclass and subclass.
Your answer

Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Push GUI button, change another objects variable 2 Answers
Prefab inheritance without scripts. Is it possible? 2 Answers
how to access list from another class (list of cards on deck class) ? 1 Answer
Which Game Topic Should I choose? 3 Answers