In nearly every project I need a Spawner.
Here is a link to a very good tutorial/demo which is a perfect starting point to create a spawner.
https://unity3d.com/de/learn/tutorials/projects/survival-shooter/more-enemies
And here is my latest template sourcecode of a spawner.
using UnityEngine; using System.Collections; using System; //This allows the IComparable Interface //This is the class you will be storing //in the different collections. In order to use //a collection's Sort() method, this class needs to //implement the IComparable interface. public class VisualSteps : IComparable<VisualSteps> { public string dummyName; public int position; public GameObject stepCube; private float stepLength = 3; // Will be overwritten, if the OnCreate GameObject has different sizes! private float stepHeight = 0.5f; // Will be overwritten, if the OnCreate GameObject has different sizes! public VisualSteps(string newDummyName, int newPosition, GameObject newGameObject) { dummyName = newDummyName; position = newPosition; stepCube = newGameObject; stepLength = newGameObject.transform.localScale.z; stepHeight = newGameObject.transform.localScale.y; ModifyStepForPosition(position); } public VisualSteps(string newDummyName, int newPosition) { dummyName = newDummyName; position = newPosition; stepCube = GameObject.CreatePrimitive(PrimitiveType.Cube); ModifyStepForPosition(position); } private void ModifyStepForPosition(int stepPosition) { stepCube.transform.localScale = new Vector3(50-stepPosition, stepHeight, stepLength); stepCube.transform.position = new Vector3(0, -10+(stepPosition* stepHeight), (stepPosition * stepLength)); ChangeColorOfStep(stepPosition); } private void ChangeColorOfStep(int stepPosition) { stepCube.GetComponent<Renderer>().material.color = UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f); } //This method is required by the IComparable //interface. //IMPORANT - can be deleted here! Not necessary for the Step Spawner... public int CompareTo(VisualSteps other) { if (other == null) { return 1; } //Return the difference in power. return position - other.position; } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpawnSteps : MonoBehaviour { public AudioClip[] meditationSounds; //public GameObject[] optionSteps; public GameObject stepPrefab; private int currentStep; List<VisualSteps> steps = new List<VisualSteps>(); void Awake() { if (meditationSounds == null) { Debug.Log("Error - no Sounds"); Debug.Break(); } else { int i = 0; foreach (AudioClip aaa in meditationSounds) { if (stepPrefab == null) { steps.Add(new VisualSteps("Step_Primitive_"+i.ToString(), i)); } else { steps.Add(new VisualSteps("Step_Prefab_" + i.ToString(), i, (GameObject)Instantiate(stepPrefab)));//, transform.position + new Vector3(spawnPointXPosition(), 1, spawnAwayRadius), Quaternion.identity);)); } //Make spawned Gameobjects Children in Unity Editor of the GameObject this script is attached to steps[i].stepCube.transform.parent = this.transform; //Add here the Sounds - or Stuff.... i = i + 1; } } } }