Advertisement
DugganSC

EnemySpawner.cs

Mar 26th, 2023
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class EnemySpawner : MonoBehaviour
  7. {
  8.     BasicEnemyMovement basicEnemyMovementScript;
  9.  
  10.     [SerializeField]
  11.     private List<GameObject> EnemyList = new List<GameObject>();
  12.     [SerializeField]
  13.     private GameObject Enemy1;
  14.     [SerializeField]
  15.     private GameObject Enemy2;
  16.     [SerializeField]
  17.     private GameObject Enemy3;
  18.  
  19.     public int EnemyIndex = 0;
  20.  
  21.     public float timer = 2;
  22.  
  23.     void Start()
  24.     {
  25.         EnemyList.Add(Instantiate(Enemy1));
  26.         EnemyList.Add(Instantiate(Enemy2));
  27.         EnemyList.Add(Instantiate(Enemy3));
  28.  
  29.         print(String.Format("There are {} enemies", EnemyList.Count));
  30.     }
  31.  
  32.     void Update()
  33.     {
  34.         timer -= Time.deltaTime;
  35.         if (timer < 0)
  36.         {
  37.             Debug.Log("Enemy timer");
  38.             ChooseNextEnemy();
  39.             timer = 2;
  40.         }
  41.  
  42.     }
  43.  
  44.     void ChooseNextEnemy()
  45.     {
  46.         if (EnemyIndex >= EnemyList.Count)
  47.         {
  48.             EnemyIndex = 0;
  49.         }
  50.  
  51.         basicEnemyMovementScript = EnemyList[EnemyIndex++].GetComponent<BasicEnemyMovement>();
  52.  
  53.         basicEnemyMovementScript.StartEnemyMovement();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement