- Home /
OnTriggerEnter not working
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 $$anonymous$$onoDevelop doesn't change to orange/ blue or whatever the default is for $$anonymous$$onoDevelop. I switched to Untron ins$$anonymous$$d and it has been working fine.
@dcrispen014: Don't post an answer when you don't answer your own question. This is a Q&A site, not a forum. I've converted your "answer" into a comment.
OnTriggerEnter is just the name of a function. It has a special meaning in a $$anonymous$$onoBehaviour class to Unity, but in a code editor it's just a function name like any other function name. It will not be highlighted since it's not a special keyword in the language itself. It's just part of the Unity API. $$anonymous$$onoDevelop is just a code-editor.
Your function will work, but only when you have a collider on the gameobject which has its "isTrigger" ticked and the collision involves a rigidbody.
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.)
Answer by ryleigh · Sep 20, 2012 at 08:03 AM
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." http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html
Answer by jonm4y · Mar 26, 2013 at 10:41 AM
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: http://www.youtube.com/watch?v=fIGEPFjm4xQ
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.
Answer by guavaman · Sep 20, 2012 at 08:36 AM
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.
Answer by unityisfun · May 19, 2013 at 03:35 AM
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(Some$$anonymous$$indOfCollider)] and it will forcibly add one for you when you attach the script if the component is not found...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Really want to get into Unity but dont understand coding 1 Answer
Grid Systems 0 Answers
Why is my game slow when i export it to Windows? 1 Answer
component activate on timer 2 Answers