The problem is that I’m creating here an array of Colliders, to get acces to script “Enemy” attached to them and execute function which slow object. After I save all colliders in class variable resetCol. Then I wait for 1.2f and want to reset speed of each object inside this array.
So problem is, that when some object is destroyed before executing last part of this code (under “yield return new WaitForSeconds(1.2f);”) - unity show me an error. As - there is no reference “NullReferenceException” and it’s ok, because in array appears empty space. So Idk how to fix it, because inside foreach I’m checking for “collider != null”, so it shouldn’t take empty space inside array, isn’t it?
So, basically, the problem is - am I checking empty statement inside array wrongly?
I probably misspoke something (have not good enough English knowledge), so ask - I’ll try to explain.
Collider[] resetCol;
public float duration;
public float slowerSlow;
IEnumerator GrowingSphere()
{
float r = 0;
float distance = 0;
while (r < duration)
{
Collider[] colliders = Physics.OverlapSphere(transform.position, distance);
r++;
distance += .074f;
foreach (Collider collider in colliders)
{
if (collider.tag == "Enemy")
{
collider.GetComponent<Enemy>().Slow(slowerSlow);
}
}
yield return new WaitForFixedUpdate();
resetCol = colliders;
}
yield return new WaitForSeconds(1.2f);
foreach (Collider collider in resetCol)
{
if (collider != null)
{
collider.GetComponent<Enemy>().speed = collider.GetComponent<Enemy>().startSpeed;
}
}
}