Search Unity

Trigger not working?

Discussion in 'Scripting' started by Sigvard, Dec 17, 2010.

Thread Status:
Not open for further replies.
  1. Sigvard

    Sigvard

    Joined:
    Mar 11, 2010
    Posts:
    44
    Okay, i had some problems with some triggers so i decided to go back to the basis to see what i was overlooking... i went all the way back to the script in the Script reference but even that is not working:

    Code (csharp):
    1. function OnTriggerEnter (hit: Collider) {
    2.     Destroy(hit.gameObject);
    3. }
    to test it, I simply put the script on a cube and made it a trigger.
    then i made a sphere above it with a rigidbody, but the sphere just falls throug the cube?!?
    i must be missing something very basic.. please help me
    Thx.
     
  2. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    Does the Cube have a Box Collider with "Is Trigger" clicked?
     
  3. Bren

    Bren

    Joined:
    Aug 28, 2008
    Posts:
    191
    Does the sphere have a collider? Is the script assigned to the cube?
     
  4. Sigvard

    Sigvard

    Joined:
    Mar 11, 2010
    Posts:
    44
    Yes the cube does have a box collider with "is trigger" enabled, and yes the sphere has a collider and the script is assigned to the cube.. you can see why im confussed
     
  5. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Does the cube with that script attached to it have a rigid body attached? I dont think you can use the OnCollisionEnter function without a Rigid Body.
     
    Rohan_Kadkol and kcfos like this.
  6. Sigvard

    Sigvard

    Joined:
    Mar 11, 2010
    Posts:
    44
    well that is correct but it is OnTriggerEnter im using and not OnCollisionEnter.
     
  7. Sigvard

    Sigvard

    Joined:
    Mar 11, 2010
    Posts:
    44
    Does no one have a solution? Cant really use Unity if OnTrigger isnt even working...
     
  8. Ezzerland

    Ezzerland

    Joined:
    Jul 1, 2010
    Posts:
    405
    You're trying to destroy your sphere as you move it into the cube?

    Have you tried adding any debugging methods to it to see if the function is being called etc?

    I will take a quick look at this when I get home (5 hours from now).
     
    luiz-f-s and GAVITRAXmz like this.
  9. Sigvard

    Sigvard

    Joined:
    Mar 11, 2010
    Posts:
    44
    well i tried with a quick "print" script but it is pretty clear thats it is not being called
     
  10. Ezzerland

    Ezzerland

    Joined:
    Jul 1, 2010
    Posts:
    405
    Both your sphere and your cube has a rigid body?

    The only other thing off the top of my head to check is that the two objects are actually colliding.

    When you placed them in your scene are you:
    a) sure that they are one above another
    b) what method is being used to move the sphere through the cube?
     
    Last edited: Dec 19, 2010
  11. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    I use OnTriggerEnter all the time. The cube trigger object doesn't need a Rigidbody, just a Collider. The sphere needs a Rigidbody + Collider.

    Code (csharp):
    1.  
    2. void OnTriggerEnter(Collider c)
    3. {
    4. Debug.Log(c.name + " triggered me");
    5. }
    6.  
     
  12. Sigvard

    Sigvard

    Joined:
    Mar 11, 2010
    Posts:
    44
    Solved! I reinstalled Unity3d and then it worked.. must have been a corrupted script or something, but thanks anyway
     
    Stonegolemstudio likes this.
  13. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    Wow, that's an annoying bug. You should probably report it in the bug reporting utility thing.
     
  14. WackyWavingInflatableArmFlailingTubeMan

    WackyWavingInflatableArmFlailingTubeMan

    Joined:
    Feb 21, 2015
    Posts:
    16
  15. Co6us

    Co6us

    Joined:
    Jan 11, 2017
    Posts:
    2
    I had the same issue, I resolved my issue by using OnTriggerEnter2D, because it is a 2D game.
     
  16. FungusMonkey

    FungusMonkey

    Joined:
    Jun 30, 2016
    Posts:
    41
    My issue was completely different. I use Visual Studio to code and I had a test project open where I was editing scripts in VS attached to that project. I started a new project to start over once I had everything in order. I was still editing the scripts form the other project and wondering why no changes. I imported the script from the other but was still editing the originals. Silly I know but make sure you have VS or that other one (Mono Develop) actually editing the file in your current project. Was driving me nuts. :)
     
    Vetebullen likes this.
  17. michaelb212

    michaelb212

    Joined:
    Jul 21, 2014
    Posts:
    8
    Thank You !!
     
  18. Batuhan13

    Batuhan13

    Joined:
    Apr 9, 2017
    Posts:
    117
    If your OnTriggerEnter not working there are a few ways to fix this firstly you need to add trigger component for 2 object ,Player and Cube then you need to enable is trigger feature for cube then you need to add rigidbody component for both object when you do these steps it will be work without any problem =)
     
    Kandare and radiantboy like this.
  19. RockingGameDeveloper

    RockingGameDeveloper

    Joined:
    May 28, 2015
    Posts:
    18
    Anyone know how to fix this in JavaScript. I am using a older version of Unity. It's a long story.
     
  20. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    Just to update as I was just working on something similar -
    OnTriggerEnter etc won't work without a RigidBody attached.

    If you use something like this with tags:

    Code (CSharp):
    1.     private void OnTriggerEnter (Collider other)
    2.     {
    3.         if (other.tag == "Traffic")
    4.         {
    5.             print (other.transform.name + "entered");
    6.             endPointColliderTrigger = true;
    7.         }
    8.  
    9.     }
    The tag must be attached to the object with the RigidBody (if you have a hierarchy for example, with an empty gameobject parent). ;)
     
  21. IGEUNITY

    IGEUNITY

    Joined:
    Aug 2, 2021
    Posts:
    4
    You still need a Rigidbody though
     
  22. IGEUNITY

    IGEUNITY

    Joined:
    Aug 2, 2021
    Posts:
    4
    Upgrade Unity. It will make using a trigger easier.
     
    GameObjectInApp likes this.
  23. unity_6p4_-Y29WwrC4A

    unity_6p4_-Y29WwrC4A

    Joined:
    Jan 9, 2022
    Posts:
    1
    If it is a trigger then it dosnt collide with stuff it just goes threw them
     
  24. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    This thread was created over 11 years ago! Let's lock it, it's just a necro magnet; please create a new thread if there's a problem for you.
     
Thread Status:
Not open for further replies.