Trigger not working? (431383)

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:

function OnTriggerEnter (hit: Collider) {
    Destroy(hit.gameObject);
}

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.

Does the Cube have a Box Collider with “Is Trigger” clicked?

1 Like

Does the sphere have a collider? Is the script assigned to the cube?

1 Like

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

1 Like

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.

4 Likes

well that is correct but it is OnTriggerEnter im using and not OnCollisionEnter.

1 Like

Does no one have a solution? Cant really use Unity if OnTrigger isnt even working…

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).

3 Likes

well i tried with a quick “print” script but it is pretty clear thats it is not being called

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?

I use OnTriggerEnter all the time. The cube trigger object doesn’t need a Rigidbody, just a Collider. The sphere needs a Rigidbody + Collider.

void OnTriggerEnter(Collider c)
{
Debug.Log(c.name + " triggered me");
}

Solved! I reinstalled Unity3d and then it worked… must have been a corrupted script or something, but thanks anyway

2 Likes

Wow, that’s an annoying bug. You should probably report it in the bug reporting utility thing.

Still not working for me

I had the same issue, I resolved my issue by using OnTriggerEnter2D, because it is a 2D game.

11 Likes

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. :slight_smile:

1 Like

Thank You !!

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 =)

2 Likes

Anyone know how to fix this in JavaScript. I am using a older version of Unity. It’s a long story.

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:

    private void OnTriggerEnter (Collider other)
    {
        if (other.tag == "Traffic")
        {
            print (other.transform.name + "entered");
            endPointColliderTrigger = true;
        }

    }

The tag must be attached to the object with the RigidBody (if you have a hierarchy for example, with an empty gameobject parent). :wink:

3 Likes