Hi, I’m having trouble trying to destroy the Enemy game object (Script 2) from Script 1, wich is a script containing common enemy data.
Script 1:
public class BaseEnemy : MonoBehaviour
{
[SerializeField]
public int health = 4;
public int speed = 0;
public void TakeDamage(int damage)
{
health -= damage;
Debug.Log("Damage Taken");
if (health <= 0)
{
Destroy(??);
}
}
}
Script 2:
public class EnemyPlayerFollow : BaseEnemy
{
public Transform player;
private Rigidbody2D rb;
private Vector2 movement;
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
}
void Update()
{
Vector3 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
rb.rotation = angle;
direction.Normalize();
movement = direction;
//if (health <= 0)
//{
// Destroy(gameObject);
//}
}
private void FixedUpdate()
{
moveCharacter(movement);
}
//public void TakeDamage(int damage)
//{
// health -= damage;
// Debug.Log("Damage Taken");
//}
void moveCharacter(Vector2 direction)
{
rb.MovePosition((Vector2)transform.position + (direction * speed * Time.deltaTime));
}
}
What I want to know, is how to modify Script 1 to Destroy Script 2 when health <=0.
Thanks.