Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnCollisionEnter() can get called before Start()

Discussion in 'Scripting' started by DaveHoskins, Apr 26, 2014.

  1. DaveHoskins

    DaveHoskins

    Joined:
    Sep 9, 2013
    Posts:
    190
    It happens when an object is created inside another collision box.
    It caught me out for a while. :rolleyes: :)
     
  2. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    Never underestimate Unity's predilection for sloppy instantiation and race conditions! :p
     
  3. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    What about Awake()? Now that one should definitely get called before any OnCollisionEnter() because it's supposed to be like the constructor.
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    File ze bug!
     
  6. DaveHoskins

    DaveHoskins

    Joined:
    Sep 9, 2013
    Posts:
    190
    Yes, using Awake fixes the problem. Although Unity seems to encourage using Start for some reason.
     
  7. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    Unity loves to confuse people! :D
     
  8. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    Unity also encourages using System.Collections rather than System.Collections.Generic which is frankly insane.

    Programmers should think of any UT recommendations and documentation as being geared towards artists and other non-coders-moonlighting-as-scripters and take them with a heaping serving of salt.

    That said, I'd say that Awake() and OnEnable() getting called immediately and colliders becoming active right away is the proper behaviour here. The problem is the lack of explicit documentation to this effect and the inherent raciness of Unity's component model that makes such documentation necessary.

    Also, this would seem to mean that things can interact on the physics layer before they are rendered, which is necessary and correct with a frame-delayed Start(), but potentially misleading. Well, I don't *think* things are rendered before Start() is called, but, yeah... The documentation doesn't go into specifics like this.