Hey guys I am stuck on a issue.
Okay I have two scripts a enemy health script and a spawner script. They are very basic however I am running into a issue that has to do with the enemy is being destroyed.
What is happening?
-
when the enemy is instantiated the “int” value numTargets gets incremented by 1;
-
When it reaches the spawn limit the enemies spawner will stop spawning.
My problem:
-I cant seem to access the spawner script from my enemyHealth script to reduce the numTargets value by one. Because I want a new enemy to spawn after the current one is defeated.
I want several different spawn points using this script and I want the enemy that was spawned from a certain spawner only access that particular game object to reduce the numTargets variable.
Note: you will notice in my code I tried to use a spawnID to identify the particular spawner I wanted the enemy to target but I couldn’t get it working.
I am stuck on this and would love any help. Scripts below:
Enemy spawner script
public class EnemySpawn : MonoBehaviour
{
public GameObject enemy;
public float spawnTimer;
public int spawnLimit;
public int spawnID;
private EnemyHealth enemyHealth;
private int spawnNum;
private float timer;
void Awake()
{
enemyHealth = enemy.GetComponent<EnemyHealth>();
}
void Start()
{
spawnID = Random.Range(1, 20);
}
void Update()
{
timer += Time.deltaTime;
SpawnEnemy();
}
void SpawnEnemy()
{
if (spawnNum < spawnLimit)
{
if (timer > spawnTimer)
{
Instantiate(enemy, transform.position, transform.rotation);
enemyHealth.spawnID = spawnID;
timer = 0f;
spawnNum++;
}
}
}
public void EnemyDestroyed()
{
spawnNum--;
}
}
Enemy Health script:
public int startingHealth = 100;
public int currentHealth;
public int spawnID;
private EnemyMovement enemyMovement;
private EnemySpawn enemySpawn;
void Awake()
{
enemyMovement = GetComponent<EnemyMovement>();
}
void OnEnable()
{
currentHealth = startingHealth;
}
void Update()
{
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
if(currentHealth <= 0)
{
Death();
}
}
void Death()
{
//Add enemy spawner script here.
Destroy(gameObject);
}
}
Are there any suggestions other than tags? Cause that would take more work and more problems.
Thank you in advance.