Laser collision not detected in Unity 2017

UPDATE: Make sure to double check that both the laser and enemy prefabs have both Rigidbody 2D and Box collider 2D components! I forgot to add the box collider2D to my lasers so they didn’t register as a colldier :slight_smile: Silly me!

Hey everyone. I’m having a hard time with detecting the laser collisions with the enemies. My code is pretty much exactly the same as demonstrated in the video:

void OnTriggerEnter2D(Collider2D collider)
{
    Projectile laser = collider.gameObject.GetComponent<Projectile>();
    if (laser)
    {
        Debug.Log("Collided with projectile");
    } 
}

But when I run the game and shoot the enemies, the console doesn’t print anything.

I already checked to make sure both the enemy prefab and the laser prefab have RigidBody2D components.
Enemy has a Box Collider 2d Component and Is Trigger is checked.
Enemies and lasers are on the same Z plane.
I’ve restarted Unity several times.

Not really sure what else to check for at this point.

Someone else who has done the course recently may be able to help you out soon (or Rob will definitely be to the rescue :D)

I don’t have the answer, but just curious as its been so long since I’ve done the course.

If instead of checking for laser, could you see what happens when you check what enters the trigger?

i.e.
void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log(collider);
}

Just want to make sure something gets triggered, if anything gets triggered when the laser goes through it. If nothing happens, I’d double check the boxcollider on the enemy and the rigidbody components on the laser.

Otherwise, check the ‘Projectile’ script on the laser, is it definitely there?

Thanks! I figured out the problem. The laser prefab didn’t have a box collider on it! Duh. Thanks for your help :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.