Unity Community

Results 1 to 17 of 17

  1. Posts

    How to use OnCollisionEnter, OnCollisionStay and OnCollisionExit


    I have made an example package explaining my problem.

    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. Posts
    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.
    Rafe [Path-o-logical Games]
    > Check out our Asset Packages
    > support@path-o-logical.com

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

    1. import UnityEngine
    2. import System.Collections
    4. class BusyCollider (MonoBehaviour):
    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")
    17.     # def OnEnable():
    18.     #     pass
    20.     def OnCollisionEnter (other as Collision):
    21.         Debug.Log("CollisionEnter")

  4. Location
    Tennessee, USA
    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. Posts
    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)

    Quote Originally Posted by steinbitglis View Post
    Case 2, OnEnable exists, OnCollisionEnter is not called (neither is OnCollisionStay)
    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 Code:
    def OnEnable():         
    true:   # <-- Always true              
    Debug.Log("oh, we didn't do that"
    Rafe [Path-o-logical Games]
    > Check out our Asset Packages
    > support@path-o-logical.com

  6. Posts
    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. Posts
    That is correct, Rafes.

  8. Posts
    It would also be helpful for me to know whether anyone get the same results.

  9. Posts
    I'm now also asking this question at answers.unity3d.com.


  10. Posts
    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. Posts
    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.
    Rafe [Path-o-logical Games]
    > Check out our Asset Packages
    > support@path-o-logical.com

  12. Location
    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. Posts
    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. Posts
    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.
    Rafe [Path-o-logical Games]
    > Check out our Asset Packages
    > support@path-o-logical.com

  15. Posts
    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. Posts
    Quote Originally Posted by steinbitglis View Post
    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.
    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).
    Rafe [Path-o-logical Games]
    > Check out our Asset Packages
    > support@path-o-logical.com

  17. Posts
    OK, I'm sorry I started this discussion. This thread is about a few misplaced tab characters, not C#.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts