Cloning prefabs
Now that we’ve tackled a game with regular C# enemies, let’s imagine we want to upgrade our game to an open world where enemies are procedurally generated at random positions on a map. You’ve already created an abstract enemy
class to store basic enemy data, which you’ve subclassed as MonoBehaviours
for each enemy type and attached to their respective prefabs. Your job is to create an enemy spawning system that can take in multiple prefabs, assign them to a prototype factory, and clone them whenever necessary.
Our first task in our new scenario is to declare a public interface in the BaseEnemy.cs
file with a method for copying prefabs, as shown in the following code. We’ll also have the abstract BaseEnemy
class implement the new interface and add custom cloning logic, with each enemy Prefab randomly placed in the scene and parented to the Enemy Spawner
object that already exists in the Hierarchy:
using System.Collections...