Why do game engines like Unity and Unreal favour components over deep inheritance trees for game objects?
What the interviewer wants to hear
- Deep hierarchies become rigid when an object needs behaviour from two branches
- Components are small reusable pieces of behaviour attached to an object
- Designers can mix components to create new object types without code
- Components are easier to test and reuse across projects
- Inheritance is still useful for shallow base classes
Model answer
With inheritance you quickly get trees like Enemy, FlyingEnemy, ShootingEnemy, and then you need a flying shooting enemy and the tree breaks. Components solve this through composition: an object is a container, and behaviour comes from small pieces like Health, Mover, Shooter or Flyer. You build a new enemy by attaching the right components, and designers can do it in the editor without new code. Each component has one job, so it is easier to reuse, test and debug. Unity GameObjects with MonoBehaviours and Unreal Actors with ActorComponents both work this way. I still use inheritance, but keep it shallow, for example a base Weapon class with a few subclasses, and put shared behaviour in components or interfaces.
Can you answer it under time pressure?
Take a timed easy round and get scored on every answer.
More easy Game Developer questions
- What is a game loop, and why is delta time important?
- In Unity, what is the difference between Awake, Start, Update, FixedUpdate and LateUpdate?
- How do you use vectors, dot product and cross product in game development?
- How does collision detection work in a game engine, and what is the difference between a collider and a trigger?
- Tell me about a game or game feature you built. What was the hardest problem and how did you solve it?
- How would you set up version control for a small game team with programmers and artists?
- What do you do as a programmer to make a game "feel" good to play?
- What are coroutines in Unity, and when would you use them instead of Update or async/await?
- How do you use prefabs, nested prefabs and prefab variants to keep a Unity project organised?