Still not getting triggering of colliders

My player has 2 colliders, 1 for standing and one for crouching/crawling
my gun has a box2D collider set to trigger

which has been moved away from it, the idea being that when the player gets within range,the gun starts firing.

this code

 private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag=="Player")
        {
            Debug.Log("Fire!");
            canFire = true;
        }
    }

only works if i add another collider to my player and set it as trigger.but thats not what i want
How can i have the collider on the gun detect when the player has crossed it?

does your player or your gun have rigidbody?

check the rules
https://docs.unity3d.com/Manual/CollidersOverview.html

yes they both have

It has to be something super-simple because we know Triggers work. Roll some simple test scripts to get it working on a sphere dropped into a cube that is a trigger area, simplify everything.

Are you using layers btw? You can mark layers not to interact in the physics settings, so maybe you did that?

Another thing to look at is how easy it to put the wrong thing on the wrong GameObject. The OnTriggerEnter has to be where the collider is, etc.

Hi kurt,

its really strange
both are on the default layer, both have rigidbodies, i even tried creating a new layer and putting them both on the same and checking thats its checked in the pyramid thing. which it is.

void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log(“this is printing”);
if (collision.gameObject.tag == “Player”)
{
Debug.Log(“shoot”);
canFire = true;
}
}

its getting into the method, as i can print when i hit the collider, it just wont check the tag for some reason.
tag is Player, even copied and pasted it.

Any other ideas?

You’re depriving yourself of critical information.

Instead print the name of collision.other and collision.gameObject.

One of them is probably not what you think it is.

Print its tag too while you’re at it… no extra charge! What is it?

As a side note you probably should call a collider2d a collider rather than a collision. :slight_smile:

Ha Ha! got it! man that was some useful stuff! The collider was my standing collider which had no tag set. Bingo! Man I wish I had your skills! Nice one! cant thank you enough!

"As a side note you probably should call a collider2d a collider rather than a collision. "

you mean this right?

void OnTriggerEnter2D(Collider2D collision)

why does the OnTrigger method auto name the parameter collision?

Thanks, and of course you’re welcome.

To me it is just relentlessly follow the data, follow every clue… it’s like “hey, my house sprang a leak somewhere and I won’t stop until I find the pipe or the roof with the hole in it…” Not in this panel? Rip it away, look behind it. Not there? Keep ripping! Coming out in the garage now? Destroy the garage, did it go away? No? It moved to the outhouse? Destroy the outhouse! Ah, it stopped… now we know it was in the outhouse. Use source control to instanlty revert EVERYTHING else you tore apart and go straight to the outhouse to find the leak.

As a side note one of my current longest-lived bugs was a bug I wrote in 1995 in my Gravity Well game, and when I was running that same newly-recompiled code in 2018 it crashed hard… turns out I used an array of 16-byte structures to hold 32-byte structures and until the screen resolution got wide enough, it never overflowed the fixed-size buffer. GOTCHA! Found the crash, fixed it. (There was guard code to check for overflow, but it thought the buffer was 2x larger than it needed to be so it never fired.)

Oh well that’s a Unity documentation boo-boo!! I thought you just mistyped it as I have in the past, since some of those responder functions take collisions and some take colliders.

I think I would still rename it personally. It doesn’t matter to the functionality but it clarifies what is truly going on. You’re getting a collider, not a collision.

1 Like