Spawning Enemies with the Prototype Pattern
In the last chapter, we dove head-first into our first design pattern – the Singleton. We talked about its strengths, weaknesses, and practical implementations with C# and Unity-specific APIs (while simultaneously trying not to vilify the poor thing). In this chapter, we’ll learn how the Prototype pattern can help you define and manage objects you want to copy while avoiding the computational overhead that comes with creating new objects (because we should all aim to be frugal programmers when it comes to memory).
The idea is to take the original object (called a – you guessed it – Prototype or prototypical instance) and return copies of it whenever needed. Think of games where procedural enemies are spawned with a base state and modified later during gameplay; it would be great if we didn’t have to create a new enemy from scratch every time they popped up to harass our character.
One important...