I have an Enemy.cs, a Level.cs, and an EnemySpawner.cs. If different levels will have different numbers of enemies, which script would it be best to have the static integer in? I’m trying to use tags to keep track of the enemies, but I’m having a hard time getting it working since the enemies spawn in waves and aren’t already on the screen (like in Brick Breaker).
I’ll attach the scripts below (sorry if I’m using the wrong format for this):
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[Header("Enemy Stats")]
[SerializeField] float enemyHealth = 100;
float shotCounter;
[SerializeField] float minTimeBetweenShots = 0.2f;
[SerializeField] float maxTimeBetweenShots = 3f;
[SerializeField] int scoreValue = 150;
[Header("Laser")]
[SerializeField] GameObject projectile;
[SerializeField] float projectileSpeed = 10f;
[Header("VFX")]
[SerializeField] GameObject deathVFX;
[SerializeField] float durationOfExplosion = 1f;
[Header ("SFX")]
[SerializeField] AudioClip enemyDeathSFX;
[SerializeField] [Range(0, 1)] float enemyDeathSFXVolume = 0.5f;
[SerializeField] AudioClip enemyLaserSound;
[SerializeField] [Range(0, 1)] float enemyLaserVolume = .02f;
// Start is called before the first frame update
void Start()
{
shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}
// Update is called once per frame
void Update()
{
CountDownAndShoot();
}
private void CountDownAndShoot()
{
shotCounter -= Time.deltaTime;
if(shotCounter <= 0f)
{
Fire();
shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}
}
private void Fire()
{
GameObject enemyLaser = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
enemyLaser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
AudioSource.PlayClipAtPoint(enemyLaserSound, Camera.main.transform.position, enemyLaserVolume);
}
private void OnTriggerEnter2D(Collider2D other)
{
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
if (!damageDealer) { return; }
ProcessHit(damageDealer);
}
private void ProcessHit(DamageDealer damageDealer)
{
enemyHealth -= damageDealer.GetDamage();
damageDealer.Hit();
if (enemyHealth <= 0)
{
Die();
}
}
private void Die()
{
FindObjectOfType<GameSession>().AddToScore(scoreValue);
Destroy(gameObject);
GameObject explosion = Instantiate(deathVFX, transform.position, transform.rotation);
Destroy(explosion, durationOfExplosion);
AudioSource.PlayClipAtPoint(enemyDeathSFX, Camera.main.transform.position, enemyDeathSFXVolume);
}
}
Level.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Level : MonoBehaviour
{
[SerializeField] float delayInSeconds = 2f;
public void LoadStartMenu()
{
//Load via index
SceneManager.LoadScene(0);
}
public void LoadGame()
{
//Load via string reference
SceneManager.LoadScene(1);
FindObjectOfType<GameSession>().ResetGame();
}
public void LoadGameOver()
{
StartCoroutine(LoadDelay());
}
IEnumerator LoadDelay()
{
yield return new WaitForSeconds(delayInSeconds);
SceneManager.LoadScene("Game Over");
}
public void QuitGame()
{
Application.Quit();
}
}
EnemySpawner.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
[SerializeField] List<WaveConfig> waveConfigs;
[SerializeField] int startingWave = 0;
[SerializeField] bool looping = false;
// Start is called before the first frame update
IEnumerator Start()
{
do
{
yield return StartCoroutine(SpawnAllWaves());
}
while (looping);
}
private IEnumerator SpawnAllWaves()
{
for(int waveIndex = startingWave; waveIndex < waveConfigs.Count; waveIndex++)
{
var currentWave = waveConfigs[waveIndex];
yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave));
}
}
private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
{
for(int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
{
var newEnemy = Instantiate(waveConfig.GetEnemyPrefab(), waveConfig.GetWaypoints()[0].transform.position, Quaternion.identity);
newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);
yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
}
}
}