Hi all! I got a little problem with my spawner. To avoid a tragic FPS drop, I would like to make him spawn enemy for as long as the enemy number is lesser than a X value, then, when the counter hit X, he would stop spawning enemies. If, for any reason, the number of enemy is lesser then X, then the spawner will start to spawn again till he reach X again, and so on. This is what i managed to do so far, and i made so much try that i can’t even remeber all of them:
using UnityEngine;
using System.Collections;
public class EnemySpawn : MonoBehaviour
{
public GameObject enemy;
public GameObject ScoreObject;
public Vector3 spawnValues;
public int enemyLimit;
public float spawnWait;
public float startWait;
public float waveWait;
void Start ()
{
StartCoroutine (SpawnWaves ());
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range (-spawnValues.z, spawnValues.z));
Quaternion spawnRotation = Quaternion.identity;
Instantiate (ScoreObject, spawnPosition, spawnRotation);
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < enemyLimit; i++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range (-spawnValues.z, spawnValues.z));
Quaternion spawnRotation = Quaternion.identity;
Instantiate (enemy, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
}
I’m sure I’m making some kind of stupid error, but I really can’t find a solution. Thank you all, I’m here for anyone who need more info!
it looks like you are missing some code to deal enemyLimit. Let say enemyLimit is set to 10. When an enemy is killed/removed for the game this value should be decreased by 1.
Right now enemyLimit is always 10
well, you have to give us more info.
When the enemy dies, do you have a peace of code that removes it from the game? it should be something like Destroy (gameObject); at this secion of code you need to access the gameObject that is running code from above. in the code above you need to add a public function. if you need help accessing the code i’ll help you out.
something like this.
public void UpdateEnemeyLimit()
{
enemyLimit = enemyLimit - 1;
}
after looking at the script. this script destroys the player object and projectile object, but not the enemy.
So I don’t think this is the correct script you needed to send me.
But I did the work anyways. so here it is.
using UnityEngine;
using System.Collections;
using DG.Tweening;
using UnityEngine.SceneManagement;
public class Destroy : MonoBehaviour
{
public GameObject Explosion;
private Transform target;
Tweener tween;
private EnemySpawn ES;
void Start()
{
target = GameObject.Find("Player").transform;
ES = GameObject.Find("nameOfGameObject").GetComponent<EnemySpawn>();;
}
void Update()
{
tween = transform.DOMove(target.position, 11.5f).SetSpeedBased().SetAutoKill(false);
tween.ChangeEndValue(target.position, true).Restart();
}
public void Delay()
{
StartCoroutine("Wait");
}
IEnumerator Wait()
{
yield return new WaitForSeconds(2);
SceneManager.LoadScene (2);
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Projectile")
{
Instantiate(Explosion, transform.position, transform.rotation);
Destroy (other.gameObject);
EP.UpdateEnemeyLimit();
Destroy (gameObject);
}
if (other.tag == "Player")
{
Instantiate(Explosion, transform.position, transform.rotation);
EP.UpdateEnemeyLimit();
Destroy (other.gameObject);
Delay ();
}
}
}
When the projectile or the player touch the enemy collider, this script actually destroy the enemy. When you read “other.gameObject”, it’s just referring to the enemy gameObject.
I can’t understand why we should decrease the enemyLimit, tough. Shouldn’t we decreare the enemyCount? And what are we suppose to look for with “nameOfGameObject”? And again, inside this script, what “UpdateEnemeyLimit()” is doing? I’m sorry for all this questions, but I’m not really experienced with coding. Thanks for your patience and help, I really apprecciate.
in the first script EnemySpawn. line 28. you are checking enemyLimit. This is the only peace of the code that creates new enemy gameObjects on line 32. so that’s what we need to decrease this value, you said you wanted to add more enemys after X had been killed.you may need more logic to get it just right.
The EnemySpawn scripts to have a peace of code added after line 37
38. public void UpdateEnemeyLimit()
39. {
40. enemyLimit = enemyLimit - 1;
41. }
“nameOfGameObject” needs to be changed to name of he gameObject that runs the EnemySpawn script.