Hello,
Hoping someone can help me out here.
I have a spawner that will spawn an enemy prefab, which creates clones of that object, so I have tagged these with “Enemy”. Next I have an arrow that, when it hits one of these enemies it will destroy that gameobj.
My problem is that if I have 3 enemies on the screen, and an arrow hits enemy2 or 3, the first enemy that was spawned is removed, and not the one that was hit with the arrow. (code below). I assume that the way I’m doing it, I’m storing the enemy game objs in a first in first out, however how do I find specifically the one that is hit with the arrow. I cannot simply do GameObject.Find(“Enemy (Clone)”).GetComponent()
void Start()
{
rb = GetComponent<Rigidbody2D>();
Destroy(gameObject,5f);
enemy = GameObject.FindGameObjectWithTag("Enemy");
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag != "Arrow")
{
//if the Arrow hits another arrow, do not delete
if (collision.gameObject.tag != "Arrow")
{
Destroy(gameObject);
if(collision.gameObject.tag == "Enemy")
{
//Debug.Log("hit enemy" + enemy);
Destroy(enemy);
}
}
}
}
Thanks in advance.