My enemy is not tirggering OnTriggerEnter2D

For some reason my tagged enemy with a box collider trigger is not triggering the OnTriggerEnter2D in my bullet script. As a test I set the ladder to an enemy, since it is a trigger as well, and it worked fine.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletBehavior : MonoBehaviour
{
    [SerializeField] float bulletSpeed;
    Rigidbody2D bulletRigidbody;
    PlayerMovement player;
    float xSpeed;

    void Start()
    {
        bulletRigidbody = GetComponent<Rigidbody2D>();
        player = FindObjectOfType<PlayerMovement>();
        xSpeed = player.transform.localScale.x * bulletSpeed;
    }

    // Update is called once per frame
    void Update()
    {
        bulletRigidbody.velocity = new Vector2(xSpeed, 0f);
    }

    void OnTriggerEnter2D (Collider2D other)
    {
        Debug.Log("wazzup?");
        if (other.tag == "Enemy")
        {
            
            Destroy(other.gameObject);
            Destroy(gameObject);
        }
        
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        Destroy(gameObject);
    }
}

Hi Beepone,

Did you share a screenshot? If so, it seems to be broken. :frowning:

Without seeing what you wanted to show, I’m just guessing what the problem might be: One of the game objects involved in the collision event does not have a trigger collider.

Also check if messages are enabled in the Console. For testing purposes, you could log a message which are definitely supposed to appear, e.g. in the Start method.

If thats your bullet selected in the inspector, its not a trigger collider, it might work if you check the box

I’ve tried that, neither worked. In the video, Rick leaves it unticked, and it works fine. The weird part is that it works when I set the ladder to the enemy, since it also uses a trigger. Both the slime and the ladder have triggers and only the ladder actually makes the OnTriggerEnter2D actually proc. The slime wont send the debug.log message even though I don’t have it nested in the Enemy tag IF statement. So I know the slime isn’t causing a trigger at all.

I shared a gif. If you click it, does it still not work? It should be a link.

The slime has a trigger collider. I’ve turned it on and off many times, but it definitely has the one because I needed one to cause the slime to turn. I even at one point set its body collider to a trigger and it still didn’t work.

It feels like unity doesn’t think my slime exists.

And what if you put it like this?

if (other.gameObject.CompareTag("Enemy")) 
{ 

}

Doesn’t work. The problem is the trigger on the slime isn’t triggering OnTriggerEnter2D. When I shoot at the slime it does not send a debug log. But I know it is working because the ladder is triggering the log. Somehow the slime, even though it has a trigger, is not triggering OnTriggerEnter2D. This feels like a bug to me. I even remade the enemy gameobject from scratch and it still doesn’t cause a trigger even though the trigger to make the object turn is working in the slime itself.

I dont remember the lecture it was on, but run through your colliders, and see if its right with this chart;

Like, for trigger one need to be a trigger, and if you have 2 triggers one needs to have a rigidbody i believe. I know i struggled with some of these things when i just started too, its quite confusing.

If this still doesnt help, if you can provide screenshots of the colliders and rigidbody, i can take another look, and maybe tell you more.

I think I found the issue, and it might just be a bug with unity. I swapped the collision layer to ladder on the slime like the ladder and it worked. I think the layout of my collision matrix is bugged because I’ve been noticing all sorts of wonk stuff like the bullet destroying itself even though I disabled it in the matrix. Right now I have it so the bullet can only pass through the NPC but it seems to be only working for the ladder layer somehow…

This doesn’t make any sense

Ah, i didnt assume the problem would be there.
Glad you found the solution anyways, good luck with your project!

From my understanding, I thought this mean’t the bullet can only trigger or collide with something on the NPC layer.

Do you know if this is a bug or something?

The way you have it there in the settings, the bullet layer can only collide/trigger with the NPC layer yes, no other layers.

In the screenshot, your slime is on the ladder layer, should be on the npc layer then.

Yes ok well it is currently colliding with pretty much everything except for the NPC somehow. Even when I turn the NPC off it still doesn’t collide with it and collides with everything. In fact, it seems nothing I change on there changes how the bullet interacts with the other layers. I restarted unity but its the same. Might just remake all my layers and see if that fixes it.

You changed your bullet layer after the first gif you sent?

1 Like

omfg… im an idiot

How did I gloss over that I was about to pull my hair out…

I can say, nearly everyone made mistakes like this when just getting started, its alot to take in :wink:
Dont worry about it.

ty so much, was about to go get a different version of unity thinking it was a bug… lol

1 Like

Privacy & Terms