Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dcrispen014 · Sep 20, 2012 at 07:43 AM · ontriggerenternew

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Sep 20, 2012 at 07:50 AM 0
Share

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.

avatar image dcrispen014 · Sep 20, 2012 at 11:34 PM 0
Share

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.

avatar image Bunny83 · Sep 20, 2012 at 11:42 PM 0
Share

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

avatar image dcrispen014 · Sep 21, 2012 at 03:27 AM 0
Share

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 Replies

· Add your reply
  • Sort: 
avatar image
7

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · May 19, 2013 at 06:26 AM 1
Share

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

avatar image whydoidoit · May 19, 2013 at 07:15 AM 1
Share

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

avatar image
0

Answer by HernandoNJ · Oct 07, 2021 at 02:13 AM

Maybe it helps

https://answers.unity.com/questions/1474449/ontriggerenter-not-working-26.html?childToView=1864967#answer-1864967

2nd reply

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

component activate on timer 2 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges