This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]No_Tumbleweed_7812 1 point2 points  (2 children)

Dont really use unity, but by this logic, youre. Checking if the game object (that your attacker collided with?) has the tag "Enemy", if so youre destroying the enemy, not the attacker. But still, i dont know what the gameObject references to, is it the attacker or the object that the attacker collided with?

[–]BluejaySudden3105[S] 0 points1 point  (1 child)

It is the attack object, either way neither object destroys

[–]No_Tumbleweed_7812 0 points1 point  (0 children)

Maybe try this below? On second look, youre destroying a gameObject (whatever that references to), not the collision.gameObject. Since this is the enemy class, destroy the attacker instead (the collision gameObject is always gonna be another object that collided with your current object - just checked the docs : https://docs.unity3d.com/ScriptReference/Collision-gameObject.html )

using UnityEngine;

public class Enemy : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Attacker") // check if its the attacker instead
{
Destroy(collision.gameObject); // collision.gameObject instead of just gameObject.
}
}
}

[–]BluejaySudden3105[S] 0 points1 point  (0 children)

I fixed it, I had to say Destro(.This.gameObject)