Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question can i get help with triggers in 2d

Discussion in 'Getting Started' started by im_a_guy, Apr 7, 2024.

  1. im_a_guy

    im_a_guy

    Joined:
    Jan 25, 2023
    Posts:
    12
    here is my script:


    using UnityEngine;
    Code (CSharp):
    1. public class threewayspawner : MonoBehaviour
    2. {
    3.  
    4. private void OnTriggerEnter2D(Collider2D other) {
    5.     if (other.gameObject.CompareTag("trigger threeway 1")){
    6.         Debug.Log("player walked past!");
    7.     }
    8.  
    9. }
    10. };
    i cant for the life of me get the triggers to even register and console log anything
    i am a complete beginner and have about 6hours of unity experience, i am using the is trigger on the colliders
     
    Last edited: Apr 7, 2024
  2. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    282
    To make physics methods like OnTriggerXXX, OnCollisionXXX, etc. work, both objects need to have colliders, and at least one of them needs to have a Rigidbody. If your objects only have colliders, these physics callbacks will not be invoked.
     
  3. im_a_guy

    im_a_guy

    Joined:
    Jan 25, 2023
    Posts:
    12
    one of my objects have a rigid body
     
  4. im_a_guy

    im_a_guy

    Joined:
    Jan 25, 2023
    Posts:
    12
    my player:
    upload_2024-4-7_9-57-31.png
     
  5. im_a_guy

    im_a_guy

    Joined:
    Jan 25, 2023
    Posts:
    12
    my trigger:
    image_2024-04-07_104841192.png
     
    Last edited: Apr 7, 2024
  6. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    819
    You've hidden the most important part in that screenshot - is "IsTrigger" enabled in the trigger collider?

    If I were you I'd also remove the additional check in your method to rule out typos in your tag

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other) {
    2.  
    3.         Debug.Log("trigger", other.transform);
    4.  
    5. }
    You can add it back in after you get things working.

    And make sure that both objects are on layers that are set to collider in the project settings

    https://docs.unity3d.com/Manual/LayerBasedCollision.html
     
  7. im_a_guy

    im_a_guy

    Joined:
    Jan 25, 2023
    Posts:
    12
    when you talk about the layers do you mean that it needs to be on the same layer? And the is trigger is selected. sadly it still didnt work when i put both of them on the same layer, and updated the code to the one posted above
     
  8. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    819
    The layers need to be set to interact/collide with each other in the Layer Collision Matrix

    Edit > Project Settings > Physics2D

    Worth checking. Although if you've never changed the layer matrix, it's likely that this isn't the problem.
     
  9. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    819
    (You've definitely attached your script to the object?)
     
  10. im_a_guy

    im_a_guy

    Joined:
    Jan 25, 2023
    Posts:
    12
    i made the green trigger work, since it has the script. but i want the green box to read when i touch the yellow one. upload_2024-4-7_11-58-47.png
     
  11. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    819
    Now that it's working, you can reintroduce your Tag check. But note that the tag of the yellow object is "Finish" not "trigger threeway 1"

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other) {
    2.     if (other.gameObject.CompareTag("Finish")){
    3.         Debug.Log("FINISH!", other.transform);
    4.     }
    5. }
    6.  
    (Of course you could attached the script to the Finish object instead, and check against the tag of the green object)
     
  12. im_a_guy

    im_a_guy

    Joined:
    Jan 25, 2023
    Posts:
    12
    i made it work, although i didnt use the code you submitted, thx for the help anyways