Putting a cap on enemies spawned
Trying to set a cap on the amount of enemies spawned by having a public variable called 'enemiesSpawned' shared between two scripts. Not only do I not know how to script it to keep the cap at, say, 25, but getting the component between the two scripts simply isn't working. (C#, trying to use gameObject.GetComponent<>().variable but not working). The first script is my enemy spawning script, and the second is the script that contains the die method, of which I'm planning on decreasing the variable enemiesSpawned by 1 each time it is executed to keep the number of enemies spawned at or below 25.
using UnityEngine;
public class EnemySpawn : MonoBehaviour
{
public GameObject enemy;
public float spawnTime = 3f;
public Transform[] spawnPoints;
void Start()
{
InvokeRepeating("Spawn", spawnTime, spawnTime);
}
void Spawn()
{
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Target : MonoBehaviour
{
public float TargetHealth = 50f;
public float enemiesSpawned;
public void TakeDamage(float amount)
{
TargetHealth -= amount;
if (TargetHealth <= 0f)
{
Die();
}
}
public void Die()
{
Destroy(gameObject);
enemiesSpawned--;
}
}
Answer by TheAltbacker · Dec 12, 2017 at 03:57 PM
I'd put the enemiesSpawned variable in the EnemySpawn class as, if I understand well, Target has multiple instances, so there would be multiple different values for each enemy. You should be able to get a reference to the 'EnemySpawn' class using:
public EnemySpawn enemySpawn;
//you can then drag and drop the relevant GameObject onto this script to get the reference
//
//
// then in void Die()
enemySpawn.enemiesSpawned -= 1;
It is also important to increase enemiesSpawned when you are spawning them.
As for capping the spawns, I believe having the Spawn() method check for the number of enemies before spawning is the easiest.
void Spawn()
{
if (enemiesSpawned <= 24)
{
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
This should solve your problem, although I haven't tested it. Hope it helps anyways.
Answer by Full8uster · Dec 12, 2017 at 05:01 PM
Try create these vars on your EnemySpawn script. Set enemy as your enemy gameobject.
public Target targetScript;
public GameObject enemy;
On Start:
targetScript = enemy.GetComponent<Target>() as Target;
Then to define a cap to the enemies that will be Instantiate create a var on EnemySpawn and add++ to each enemy instanted:
public var enemiesSpawned;
On the Spawn method:
void Spawn()
{
if (enemiesSpawned <= 25){
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
enemiesSpawned++;
}
}
Then to acess your other script is just yse the targetScript var to get and set values from it.
I don't know if it is exactly what you want, but hope helped somehow.
If you need me, I'm at your disposal!