Вот фрагмент моего кода:
private void Cross()
{
animator.SetTrigger("cross");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, Firerange2, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<Enemyhealth>().TakeDamage(crossdamage);
if (enemy == null)
{
return;
}
enemy.GetComponent<EnemyStuff>().TakeDamage(crossdamage);
if (enemy == null)
{
return;
}
}
}
В этом коде скрипт Enemyhealth повреждается, а EnemyStuff - нет. Если я переставлю его так:
private void Cross()
{
animator.SetTrigger("cross");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, Firerange2, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<EnemyStuff>().TakeDamage(crossdamage);
if (enemy == null)
{
return;
}
enemy.GetComponent<Enemyhealth>().TakeDamage(crossdamage);
if (enemy == null)
{
return;
}
}
}
, скрипт EnemyStuff будет поврежден, а «ownHealth» - нет. Это мой EnemyStuff TakeDamage void:
public void TakeDamage(int damage)
{
currentHealth -= damage;
// play hurt animaton
if (damage == 10)
{
animator.SetTrigger("jab");
if (LookAtCharacter.facingRight == true)
{
rb.AddForce(new Vector2(-150, 0));
}
else if (LookAtCharacter.facingRight == false)
{
rb.AddForce(new Vector2(150, 0));
}
}
if (damage == 15)
{
animator.SetTrigger("uppercut");
if (LookAtCharacter.facingRight == false)
{
rb.AddForce(new Vector2(200, 130));
}
else if (LookAtCharacter.facingRight == true)
{
rb.AddForce(new Vector2(-200, 130));
}
}
if (damage == 20)
{
animator.SetTrigger("cross");
if (LookAtCharacter.facingRight == false)
{
rb.AddForce(new Vector2(200, 0));
}
else if (LookAtCharacter.facingRight== true)
{
rb.AddForce(new Vector2(-200, 0));
}
}
}
this is my Enemyhe take damage void: publi c void TakeDamage (int damage) {currentHealth - = damage;
// play hurt animaton
if (damage == 10)
{
animator.SetTrigger("hurt");
if (enemyscript.facingRight == true)
{
rb.AddForce(new Vector2(-150, 0));
}
else if (enemyscript.facingRight == false)
{
rb.AddForce(new Vector2(150, 0));
}
}
if (damage == 15)
{
animator.SetTrigger("hurt");
if (enemyscript.facingRight == false)
{
rb.AddForce(new Vector2(200, 130));
}
else if (enemyscript.facingRight == true)
{
rb.AddForce(new Vector2(-200, 130));
}
}
if (damage == 20)
{
animator.SetTrigger("hurt");
if (enemyscript.facingRight == false)
{
rb.AddForce(new Vector2(200, 0));
}
else if (enemyscript.facingRight == true)
{
rb.AddForce(new Vector2(-200, 0));
}
}
}
Как мне исправить это?