protected IEnumerator SpawnEnemies() { while (numEnemies > 0) { GameObject enemy = Instantiate(enemyPrefab, transform.position, Quaternion.identity) as GameObject; AreaFollow areaFollow = enemy.GetComponent <AreaFollow>(); areaFollow.targetTrigger = targetTrigger; if (eventGateObject) { EventGateObject enemyEventGateObject = enemy.AddComponent(typeof(EventGateObject)) as EventGateObject; enemyEventGateObject.eventGate = eventGateObject.eventGate; } --numEnemies; yield return(new WaitForSeconds(delay)); } Destroy(gameObject); }
// Use this for initialization void OnEnable() { eventGateObject = GetComponent <EventGateObject>(); StartCoroutine(SpawnEnemies()); }