- Home /
inheritance and static properties
Hi,
I would like to clarify the uses of inheritance class and static properties before I encounter a problem.
My game is a boat game with several levels. On those levels there is 2 differents kind of water : deep water and shallow water. One level will have a deep water or a shallow water, never both. To create them this is what I have done :
One parent class Water (inherited from MonoBehaviour) that contains every attributes common to all waters (mesh and mesh data mostly)
One class DeepWater (inherited from Water) with function and attributes dedicated for this kind of water
One class ShallowWater (inherited from Water) with function and attributes dedicated for this kind of water
Up to here I think everything is OK.
The trick is that all the attribute in Water are static. They are all defined in awake by either DeepWater or ShallowWater. I did that so every object can access basic variables without looking for a special instance. For example, the ship acces the mesh (which is a static property of Water) simply with Water.mesh
. I will use this code no matter if the water is deep or shallow.
Will it work ?
I’m pretty new to object oriented programming, so please don’t mind saying obvious things ☺
Answer by sacredgeometry · Mar 30, 2020 at 06:19 PM
Hi there, I suggest you dont use static members like this nor inheritance:
I also suggest maybe learn some object oriented design patterns:
The strategy pattern seems like a good fit:
https://www.youtube.com/watch?v=v9ejT8FO-7I
The gang of four design patterns book is almost essential reading for beginners at this point.
Your answer

Follow this Question
Related Questions
Having static classes inherit from a static class 1 Answer
Abstract Class acting like a static Class when inherited 1 Answer
Question about static variables and inheritance 2 Answers
Access variables,method from a Mono Behaviour without static use. -1 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer