Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to use OnCollisionEnter, OnCollisionStay and OnCollisionExit

Discussion in 'Scripting' started by steinbitglis, Sep 23, 2011.

  1. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    Hi!

    I have made an example package explaining my problem.
    View attachment $TestOnCollisionEnter.unitypackage

    What happens is that whenever I include OnEnable() in my script, I'm unable to respond to collision events.

    I also expect to be able to use WaitForSeconds() and at the same time receive collision events.

    Any help is appreciated.
     
  2. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Hi, Can you get all the information in to this post? I'd love to have a look but I'm on Linux and short on time. If not, I'm sure someone will be able to look at your package.
     
  3. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    There are two boxes colliding, one of them has the following script.

    Case 1, this is the code, OnCollisionEnter is called

    Case 2, OnEnable exists, OnCollisionEnter is not called (neither is OnCollisionStay)

    Case 3, OnEnable starts a coroutine that prints after the boxes collide. Result is thet same as Case 2.

    Code (csharp):
    1. import UnityEngine
    2. import System.Collections
    3.  
    4. class BusyCollider (MonoBehaviour):
    5.  
    6.     # def waitAndSee() as IEnumerator:
    7.     #     Debug.Log("waiting...")
    8.     #     yield WaitForSeconds(2.0f)
    9.     #     Debug.Log("done!")
    10.     #
    11.     # def OnEnable():
    12.     #     if true:
    13.     #         StartCoroutine(waitAndSee())
    14.     #     else:
    15.     #         Debug.Log("oh, we didn't do that")
    16.  
    17.     # def OnEnable():
    18.     #     pass
    19.  
    20.     def OnCollisionEnter (other as Collision):
    21.         Debug.Log("CollisionEnter")
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Its a bit confusing, what are you expecting from OnEnable? This should be thrown anytime that the object is disabled then re-enabled, and probably when the model begins its existence. OnCollisionEnter and Stay are relatively static. They are going to throw anytime that the physics engine calculates and collision.

    How are you moving things? (Physics or Math?)
     
  5. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I miss Python! (Though, I have to admit it is easier to know what is going on in C# and it has a lot of similar formatting, believe it or not)

    So, are you saying that with the above code:
    * When you uncomment OnEnable: pass, and your object touches another collider, that OnCollisionEnter doesn't get called, AND...
    * With the exact same code and collision in your scene, but with OnEnable commented out, it DOES work?


    BTW, this will always run the co-routine. I'm not sure why you have an if statement here
    PHP:
    def OnEnable():         
        if 
    true:   # <-- Always true              
            
    StartCoroutine(waitAndSee())          
        else:              
            
    Debug.Log("oh, we didn't do that")
     
  6. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    It's all in the example package. One of the boxes is falling by gravity, the other is kinetic. Both have a collider and a rigidbody.

    Nevermind the else clause. I was experimenting with calling StartCoroutine, but it turned out that the existence of OnEnable was enough to confuse the collision system.
     
  7. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    That is correct, Rafes.
     
  8. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    It would also be helpful for me to know whether anyone get the same results.
     
  9. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
  10. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    Sorry for the bump, this is the last time.

    This is growing more and more into a serious problem for us. It's only 6 lines of code, two boxes and nothing out of the ordinary. If I can't understand what happens in this case, there's no way we'll be able to use Unity in our project.
     
  11. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I meant to try this but got too busy. I would try it in a completely new project with no imported assets and in C#. If you can reproduce with this scenario, I would call Unity directly, or send a direct email at the least.

    ...and please share what you find out.
     
  12. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,050
    I downloaded the package. Unfortunately I don't know Boo, but it looks more like an issue with Boo syntax than anything else.

    If you place 'OnCollisionEnter' before 'OnEnable' and before anything else, then it gets called as expected. Although so too does 'OnEnable' (I add a debug.log to it) and thats after the collision, which is weird.

    To me it looks like syntax issue, with code flow falling through the functions or something.
     
  13. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    Thank you so much for looking through this! As it turns out, the boo script templates (from the "create" menu) contain tab characters. A few of those characters survived my editing and confused me (for days literally). I know it's also my own fault, so this is seriously embarrassing.

    I have made changes to my editor (Vim) now, so that I do not run into this problem again. This is the second time I get burned by these templates since I started using Unity and boo script.
     
  14. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I highly recommend switching to C#. I taught Python, and love it, and I can tell you, once you get used to the basics, C# is nearly as clean and much easier to see what is going on (most of the time). Python is much more flexbile and easier to extend and wrap things, but in this environment (Unity), C# is the only language that is clear and has no limitations introduced by the language.

    It may feel painful at first, but it will save you countless issues and debugging later on.
     
  15. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    The only language that is clear? Python? Sorry, but your recommendation seems misled. I'm sure you just don't know what you've missed.

    Thanks for the help anyway, after rewriting a minimal sample to C# I could rule out all potential language related errors. My real problem was not tab characters, but an assumption about the physics engine. The tab characters just happened to ruin my minimal example, and that freaked me out a little.
     
  16. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Was this directed at me? I didn't say that. What I DID say was:

    "C# is the only language that is clear AND has no limitations introduced by the language." (I capitalized "and" here)

    This is a true and correct statement, not an opinion (I guess the 'clear' part is, sorry to mix that in). People may like other languages and don't need the extra capabilities of C#, but that doesn't change the facts.

    I have no specific love of C# over other languages. I have used Python, JavaScript, MEL, VBScript, PHP, HTML, XML, a couple of completely proprietary languages, and a few others. I don't want to spark an opinion-driven thread; there are plenty of those. I'm just sharing a fact based on my experience with Unity and the languages that are available. I'm sure you will be fine with boo (unless you want to use iOS). I'm sure others will be fine with JavaScript too (without 'out' or 'ref' values, I believe?). It is all preference and you can use what you want, but it doesn't hurt to know what the lay of the land is. I wish I could find a reference for comparison. That would be wonderful. I just know there have been several cases where I could only have created the solution with C# (I'm sorry i can't recall them now).
     
  17. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    OK, I'm sorry I started this discussion. This thread is about a few misplaced tab characters, not C#.