- Home /
Problem with OnTriggerEnter not triggering
Hello, I'm actually having troubles with a very easy task. Whenever an enemy dies, it spawns a "pick up" item; this item has the following script attached:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PowerUpBehaviour : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
//do something
Destroy(this.gameObject);
}
}
}
So the idea is: once spawned, the item gets destroyed if the player runs over it. The player has a rigidbody and collider, while the pick up item only has a collider set as "is trigger". Well, it doesn't work. The pick up item doesn't destroy. What's wrong? Both items have colliders and at least one (the player) has a rigidbody attached. Thank you in advance.
Is it a 2D or 3D game (i.e are you using 2D Physics component or 3D ones?) If you are using 2D ones, you need to implement
OnTriggerEnter2D
Are there any static / kinematic objects involved?
Are you sure
OnTriggerEnter
is not called? Put aDebug.Log(other.name + " has entered the trigger", this);
outside of theif
Game is 3D. The objects involved are the player (with collider and rigidbody) and the pick up item (with just a collider and "is trigger" enabled). The player's tag is correctly set to "Player". I tried to put the Debug.Log to double check and I confirm it's not called.
Answer by Jellogramming · Sep 18, 2021 at 07:10 PM
Try this:
if (other.gameObject.tag == "player"){
//do something
Destroy(this.gameObject);
}
Hello, unfortunately it doesn't work. I tried that first, then changed to CompareTag("Player") due to desperation. Nothing. Thanks for your reply anyway.