Can I use OnCollisionEnter2D instead of OnTriggerEnter2D

When I was a kid, Space Invaders gave me nightmares (literally). Yeah, I am that old. So I decided to make my Laser Defender a game with birds and balls instead of ships and lasers. All was well up until I decided that I had learned enough to make the ball hit the bird and have the bird bounce away from the ball. This doesn’t work with OnTriggerEnter2D, because apparently if “is Trigger” is True, then the bird will not bounce away from the ball. So I tried switching OnTriggerEnter2D to OnCollisionEnter2D. Now the bird will bounce away, but the subroutine is never called and the bird is never destroyed. Any ideas as to what I could have done wrong?

Here is part of the script:

 private void OncollisionEnter2D(Collision2D other)
    {
        Debug.Log("Collision Happened!");
      
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
        if(!damageDealer) { return; }
        ProcessHit(damageDealer);
    }

Collison Happened! is never reported in the console.

Thanks!

Hi,

Have you already added Debug.Logs to your other class where the bird is supposed to get destroyed? Which subroutine do you mean?

I am so new at this, I am not sure that I totally understand your questions, so if I seem confused it is probably because I am.

OnCollisionEnter2D is in two places, this one is from the Enemy script, but I added the Debug.Log to the player one as well. Neither one print. I thought that maybe I missed something about OnCollisionEnter2D that would make this not work, but I can’t find anything…

The Debug.Log(“Fire”) does work.
Here is the whole Enemy script:

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

public class Enemy : MonoBehaviour
{
    [SerializeField] float health = 100;
    [SerializeField] float shotCounter;
    [SerializeField] float minTimeBetweenShots = 0.2f;
    [SerializeField] float maxTimeBetweenShots = 3f;
    [SerializeField] bool shouldIShoot = true;
    [SerializeField] int randomizeShooting = 100;
    [SerializeField] int percentageOfBirdsWhoShoot = 30;
  

    [SerializeField] GameObject splatprefab;

    // Start is called before the first frame update
    void Start()
    {
        shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
        
    }

    // Update is called once per frame
    void Update()
    {
        Shooting();
    }

    private void Shooting()
    {
        if (shouldIShoot == true)
        {
            CountDownAndShoot();
        }
        if (Random.Range(1, randomizeShooting) <= percentageOfBirdsWhoShoot)
        {
            shouldIShoot = true;
        }
        else
        {
            shouldIShoot = false;
        }
    }

    private void CountDownAndShoot()
    {
        shotCounter -= Time.deltaTime;
        if (shotCounter <= 0f)
        {
            Fire();
            shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
        }
    }

    private void Fire()
    {
        Debug.Log("Fire");
        GameObject splat = Instantiate(
            splatprefab,
            transform.position,
            Quaternion.identity) as GameObject;
        
    }

   private void OncollisionEnter2D(Collision2D other)
    {
        Debug.Log("Collision Happened!");
      
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
        if(!damageDealer) { return; }
        ProcessHit(damageDealer);
    }

    private void ProcessHit(DamageDealer damageDealer)
    {
        Debug.Log("ProcessHit");
        health -= damageDealer.GetDamage();
        if (health <= 0)
        {
            Destroy(gameObject);
        }
    }
}

There is a typo in your OnCollisionEnter2D method. Try to fix that. Then the collisions should hopefully work. C# is case-sensitive, so if something does not work as expected, check the spelling first. :slight_smile:

Oh thank you. Such a simple mistake, and yet I just couldn’t see it. Thanks again.

Don’t worry. That happens to all of us. :slight_smile:


See also:

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

Privacy & Terms