Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Collision function not working

Discussion in 'Scripting' started by splash24, Jun 13, 2024.

  1. splash24

    splash24

    Joined:
    May 14, 2024
    Posts:
    41
    I am following the unity tutorial, and I am following the step of collision detection. I have an object with a script that detects collision, both it and other objects have the box collider component. But when they collide the collision does not happen why is that?
    Code (CSharp):
    1. private void OnTriggerEnter(Collider other){
    2.         Destroy(gameObject);
    3.         Destroy(other.gameObject);
    4.     }
     
  2. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    241
    arkano22 likes this.
  3. splash24

    splash24

    Joined:
    May 14, 2024
    Posts:
    41
    I have all my prefabs with is trigger on

    onCollisionEnter did not work either
     
    Last edited: Jun 13, 2024
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,656
    Triggers will not collide with each other. One of them needs to not be a trigger.
     
    arkano22 likes this.
  5. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    241
    At least one of the two objects needs to have a Rigidbody component in order for the OnCollisionEnter/OnTriggerEnter functions to fire.

    Also, how are you moving the objects?
     
  6. splash24

    splash24

    Joined:
    May 14, 2024
    Posts:
    41
    I got the objects to collide with each other.
    But the trigger seems to disappear after moving for a bit.
    I thought it was the gravity, but the object still disappears with graity off.
    how do I fix this
     
  7. splash24

    splash24

    Joined:
    May 14, 2024
    Posts:
    41
    never mind, I got it thank you