Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved My first project: I could not figure out collision.

Discussion in 'Scripting' started by D4T4N0TFound, Oct 2, 2023.

  1. D4T4N0TFound

    D4T4N0TFound

    Joined:
    Sep 29, 2023
    Posts:
    5
    Hello. I am new to game development. I am trying to make a simple spaceship game. I want the alien's health to decrase when the bullet hits it. Even though the colliders and rigidbodies work and the bullet gets destroyed, alien's health does not go down. I could not figure it out why.

    Here are my scripts:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AlienScript : MonoBehaviour
    6. {
    7.     public int health = 100;
    8.  
    9.     // Called when the object is hit by a collider (e.g., a bullet)
    10.     private void OnCollisionEnter(Collision collision)
    11.     {
    12.         if (collision.gameObject.CompareTag("Bullet"))
    13.         {
    14.             health -= 50;
    15.             Debug.Log("Health: " + health);
    16.  
    17.             if (health <= 0)
    18.             {
    19.                 Debug.Log("Alien Destroyed");
    20.                 Destroy(gameObject); // Destroy the alien if health reaches or goes below 0
    21.             }
    22.         }
    23.     }
    24. }
    25.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletController : MonoBehaviour
    6. {
    7.     private void OnTriggerEnter2D(Collider2D other)
    8.     {
    9.         Destroy(gameObject);
    10.     }
    11. }
    12.  
    13.  
     
  2. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    106
    In the Alien script, try changing
    OnCollisionEnter
    to
    OnCollisionEnter2D
    . The former is for the 3D physics system. In Unity, 2D and 3D physics use different systems and have different callback methods as a result.

    If you still aren't getting any results, perform some basic debugging to determine which lines of code are being reached and which ones aren't. Debug.Log is great for this! You can of course also use breakpoints in your IDE.
     
    Last edited: Oct 3, 2023
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    Like Chubzdoomer mentioned, 2D and 3D colliders are totally separate. OnCollisionEnter will never work with any 2D colliders. Similarly OnTriggerEnter2d will never work with 3D colliders. It looks like you might be mixing the two.

    Also, there are other conditions that can affect collisions. See the graphs on this page:
    https://docs.unity3d.com/Manual/CollidersOverview.html
     
  4. D4T4N0TFound

    D4T4N0TFound

    Joined:
    Sep 29, 2023
    Posts:
    5
    Thanks for your help! Issue is solved.