Player not getting destroyed when touched by enemies

Hello! I’m trying to develop my first 2D shooter. I have 3 types of enemies and 3 types of spells which destroys one of the enemies, but if it hits a wrong enemy type just the projectile gets destroyed.

I’ve been trying to include a destroy script for when the player gets touched by an enemy, but for some reason I can’t get it going.

Here is the script for enemy prefab:

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

public float EnemySpeed;
private Transform myTransform;

void Start () {
	myTransform = transform;
}

void Update () {
	float amtToMove = EnemySpeed * Time.deltaTime;
	myTransform.Translate(Vector3.left * amtToMove);
	
	if (myTransform.position.x < -10.9f)
		Destroy(this.gameObject);
}
	
void OnTriggerEnter(Collider otherObject)
{
	if (otherObject.tag == "Wizzard") {
		Destroy (otherObject.gameObject);
		Destroy (gameObject);
		}
}

}

Have you added Colliders and a RigidBody to the object?

This is the enemy script and you want it to destroy the player? In you’re onTriggerEnter script you’re checking for the tags “EnemyArcane” “EnemyFire” and “EnemyFrost”, but theres no checking for the player. I’m guessing the player doesn’t have these tags. I think you need to change those to the players tag and make sure your player has the right tag. like this

EDIT: for 2D colliders your probably want to use the 2D physics variations

void OnTriggerEnter2D(Collider2D otherObject)
 {
     if (otherObject.tag == "PlayerTag") {
         Destroy (otherObject.gameObject);
         Destroy (gameObject);
         }
}

It might be worth using the OnTriggerEnter and destroy on the player script itself. It seems you’re just losing track of your logic a little bit. Let me know if this helps or if you can expand on the problem.

Is at least one collider in this collision a trigger?

I managed to solve it, apparently the player had to be a prefab as well. Thanks for your help guys! Have a nice day! :slight_smile: