OnTriggerEnter not working (51871)

Hey there. I’m pretty new to Unity but I am learning how to code now. The problem is the “OnTriggerEnter” function is not working. It doesn’t even change color. I got the code from a youtube tutorial so I know it works. Heres the part of the code.

function OnTriggerEnter(other : Collider){

What doesn't change colour? I'm guessing there's some more code: edit your question, paste the code, highlight it and click the 101 button.

Hey there. The actual lettering of "OnTriggerEnter" within MonoDevelop doesn't change to orange/ blue or whatever the default is for MonoDevelop. I switched to Untron instead and it has been working fine.

Sorry my bad I (clearly) am new here I'll try not to do that anymore. But thank you for the advice I did convert it to a rigidbody and that fixed everything. (sorry if that comes off sarcastic I tend to type that way but I appreciate all of your as well as everyone's help.)

5 Answers

5

Do you have a Rigidbody component on one of the two objects? I believe at least one of them needs to have one.

“Note that trigger events are only sent if one of the colliders also has a rigid body attached.”

Check this link out: Unity3D Documentation - Physics

Scroll down to the section “Collision action matrix”. This chart will tell you what types of colliders can set off the different trigger types.

I just ran into this problem as well. In Hierarchy view, I have a parent object with one child object with the same name. Looks like I inadvertently assigned the script handling the OnTriggerEnter event to the parent object, but the collider to the child object. Adding collider to the parent object fixed it. I’m still very new, but I am surprised that having an OnTriggerEnter event in a script attached to object without collider does not generate a compile error or warning message.

(Unity Version 4.1.2f1)

Nope it will never generate a warning or error, because that's not how Unity operates with its component model. At any moment during gameplay you could add a collider or remove a collider or add a rigidbody to that object (or enable or disable them).

I should point out that your could flag your script as [ReqiureComponent(SomeKindOfCollider)] and it will forcibly add one for you when you attach the script if the component is not found...

Maybe it helps

2nd reply

OK, sorry I’m jumping on the band wagon a bit here…but I have just run into this problem myself!

heres the code im using:

var Sound : AudioClip;
function OnTriggerEnter(){ audio.PlayOneShot(Sound);
}

I have been following this tutorial on activating sound with a trigger: - YouTube

I followed exactly as he does, but when it comes to entering the code, (at 3:01) my OnTriggerEnter wont change blue. (and consequently doesn’t work)

Any ideas?

Oh and I’ve tried adding a Rigidbody on as mentioned above and that doesn’t work either.