I'm getting some attacking to work with my units and I noticed that the coroutines seem to start adding up even after the StopCoroutine(IEnumerator) as need called.
I dug around on ways to perform an attack loop, most of the time I found "Use InvokeRepeating" and "Don't use InvokeRepeating". Since invoke uses reflection it's performance heavy anyways, so I'd rather not go down that road.
How do I get my coroutine to stop? Is it because it's in a while loop inside the coroutine? If so, how do I fashion an attack loop without using invoke?
//Recieves call when a new enemy is added to the list
public void NewEnemyDetected()
{
if(currentlyAttacking == null)
{
SelectEnemyToAttack();
}
}
//Recieves call when an enemy leaves the collider area
public void EnemyGone()
{
SelectEnemyToAttack();
}
//Will select an enemy from the array to attack
private void SelectEnemyToAttack()
{
if(enemyObjects.Count > 0)
{
StopCoroutine(AttackEnemy());
currentlyAttacking = enemyObjects.ElementAt(0);
isAttacking = true;
StartCoroutine(AttackEnemy());
}
else
{
currentlyAttacking = null;
isAttacking = false;
}
}
//Partially hacked together attacking loop
private IEnumerator AttackEnemy()
{
while(isAttacking)
{
Vector3 angle = currentlyAttacking.DetectedGameObject.transform.position - transformV.position;
angle = angle.normalized;
GameObject projectile = (GameObject)Instantiate(AttackType, transformV.position, transformV.rotation);
BlasterAttack projectileAttack = projectile.GetComponent<BlasterAttack>();
projectileAttack.OnCreation(10, player);
projectile.GetComponent<Rigidbody>().AddForce(angle * 100);
yield return new WaitForSeconds(1);
}
yield return false;
}
Thanks!