Добрый день!
Подскажите, как исправить ошибку в корутине:
MissingReferenceException: The object of type 'SpriteRenderer' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.SpriteRenderer.set_color (UnityEngine.Color value) (at :0)
Hero+d__43.MoveNext () (at Assets/Scripts/Hero.cs:195)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/bokken/build/output/unity/unity/Runtime/Export/Scripting/Coroutines.cs:17)
Понимаю, что нужно сделать проверку, но как реализовать не знаю.
Объект меняет цвет при ударе:
private IEnumerator EnemyOnAttack(Collider2D enemy)
{
SpriteRenderer enemyColor = enemy.GetComponentInChildren<SpriteRenderer>();
enemyColor.color = new Color(0.98828125f, 0.328125f, 0.2578125f);
yield return new WaitForSeconds(0.4f);
enemyColor.color = new Color(1, 1, 1);
}
Вызывается:
private void OnAttack()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(attackPos.position, attackRange, enemy);
if (colliders.Length == 0)
missAttack.Play();
else
attackMob.Play();
for(int i = 0; i < colliders.Length; i++)
{
colliders[i].GetComponent<Entity>().GetDamage();
StartCoroutine(EnemyOnAttack(colliders[i]));
}
}