Search Unity

OnCollisionEnter() Not Working

Discussion in 'Editor & General Support' started by HumphreyGames, Oct 1, 2020.

  1. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    Hello :)

    I Am trying to make an attack system where when the enemy collides with the players hand, he dies. I have added a "Debug.Log" when he collides with the hand, but that message is not showing up? Here is my code:

    void OnCollisionEnter(Collision col)
    {
    if (col.gameObject.CompareTag("Impact"))
    {
    Debug.Log("Hit");
    }
    }

    I have added a box collider to the players hand and tagged it with "Impact". Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Start with the API reference for OnCollisionEnter() and make sure you are meeting all the requirements specified.

    Also, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    Joe-Censored likes this.
  3. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    Hello! Thank You So Much! I tried doing what you said and it worked :D Thanks again!
     
    Kurt-Dekker likes this.