Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 steinbitglis · Sep 24, 2011 at 02:14 PM · collisionwaitforsecondsonenable

How to use OnCollisionEnter, OnCollisionStay and OnCollisionExit

Hi!

The following package demonstrates my problem.
TestOnCollisionEnter.unitypackage

Whenever I include OnEnable() in my script, I'm unable to respond to collision events.
I also expect to be receive collision events, while using WaitForSeconds().

The scene looks like this:

  • Two boxes collide almost immediately after the scene starts.

  • One of the boxes is falling by gravity, the other is kinetic.

  • Both boxes have a collider and a rigidbody.

  • One of the boxes have the following script.

      import UnityEngine
    
      import System.Collections
    
      class BusyCollider (MonoBehaviour):
    
          // def waitAndSee() as IEnumerator:
    
          //     Debug.Log("waiting...")
    
          //     yield WaitForSeconds(2.0f)
    
          //     Debug.Log("done!")
    
          //
    
          // def OnEnable():
    
          //     StartCoroutine(waitAndSee())
    
          // def OnEnable():
    
          //     pass
    
          def OnCollisionEnter (other as Collision):
    
              Debug.Log("CollisionEnter")
    
    

My results:

  • Case 1: using the code as it is, OnCollisionEnter() gets called.

  • Case 2: if OnEnable() exists, OnCollisionEnter() does not get called.

  • Case 3: if OnEnable() starts a coroutine that waits while the boxes collide, the result is the same as in Case 2.

Why does not OnCollisionEnter() get called? Can I also expect OnCollisionEnter() to be called in Case 3? Any help is appreciated.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by steinbitglis · Sep 26, 2011 at 09:26 PM

When using boo with Unity it's very important not to confuse tabs with spaces and the boo script templates contain tabs.

In this case, if OnCollisionEnter had less indentation than the rest, say OnEnable, then OnCollisionEnter would not be considered a part of BusyCollider leading to this confusing behaviour.

Secondly, the examples would have worked as expected, if you (I) had run them, as they are written in the question.

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 Kacer · Sep 24, 2011 at 06:36 PM

Well, i dont know if you've read the documentation yet, but here's some links:

OnCollisionEnter

OnCollisionStay

OnCollisionExit

Regarding the spammer, thats something you're going to have to live with, Also you might want to look at the date and realise that its currently saturday evening (depending on your location in the world), so it is going to take a bit longer than usual to get an answer.

Also, it would help a lot if you wrote what you want your script to do, i might just be high on caffeine and sleep deprived, but all i see is an analysis of what your script does.

Welcome to UnityAnswers, hope you have a great time, and want to contribute :)

Comment
Add comment · Show 6 · 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 steinbitglis · Sep 24, 2011 at 06:51 PM 0
Share

Thanks. $$anonymous$$y script is not supposed to do anything except for a few prints. The question is why OnCollisionEnter() does not get called.

avatar image Kacer · Sep 24, 2011 at 07:07 PM 0
Share

have you tried printing "other.gameobject.name"?

see if it can print that at least.

avatar image steinbitglis · Sep 24, 2011 at 07:43 PM 0
Share

How would that be more probable than printing a hard coded string? The problem is not an inability to print.

avatar image syclamoth · Sep 25, 2011 at 02:10 PM 0
Share

other.gameObject.name, as in the 'Other' inside of the OnCollision script!

avatar image steinbitglis · Sep 26, 2011 at 07:38 AM 0
Share

I can't even print a hard coded string. OnCollisionEnter is not being called.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to delay time reset on collision? 2 Answers

im trying to make it if a specific item collides with a cube it flashes red but it keeps comming up with an error 2 Answers

I need help making it so my countdown trigger timer doesn't start until the entire rigid body is inside the trigger 2 Answers

Wait for seconds on collision 1 Answer

Finding the exact time in a WaitForSeconds that it is currently at 2 Answers


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