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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OnTriggerEnter2d not working

Discussion in 'Scripting' started by dusthound, Sep 18, 2018.

  1. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    I am making a space game and need to detect when the player hits a collectable, i have tried using ontriggerenter2d but it is not working. Here is my code:

    void OnTriggerEnter2D(Collider2D col)
    {
    if (col.gameObject.tag == "Coin")
    {
    coins = coins + 1;
    }
    }
     
  2. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Do you have a rigidBody2d attached to either your player or coin?
     
  3. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    no, just 2d colliders
     
  4. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    You need to attach a RigidBody2d to your coin and make sure kinetic isnt selected.
     
  5. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    And make sure trigger on the coin is selected within the collider
     
  6. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    ok, thanks i will try that
     
  7. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    Thanks, that got it to work. :)
     
    shawnrevels likes this.
  8. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    You're welcome
     
  9. orhn

    orhn

    Joined:
    Mar 25, 2020
    Posts:
    1
    I have same isues .
    When I add tag from inspector, problem is solved...