2
\$\begingroup\$

When I disable an active script in unity, will it be disabled then and there, or will it only be disabled at the end of the frame?

For example, if I disable a certain script from within a "OnCollisionEnter" event method, will the "disabled" script still perform it's Update method for this frame?

\$\endgroup\$
3
\$\begingroup\$

I just tested this using the following code:

void OnCollisionEnter( Collision collision )
{
    this.enabled = false;
    Debug.Log( "Collided with " + collision.gameObject.tag );
}


void Update()
{
    if( this.enabled )
    {
        Debug.Log( "Enabled" );
        return;
    }
    Debug.Log( "Disabled" );
}

"Enabled" was displayed until the object came in contact with something, it outputted "Collided with tag". "Disabled" was never shown, proving that disabling/enabling an object takes immediate effect. It's probably just a flag that is checked when calling event methods.

This is assuming that Update methods are called after OnCollisionEnter, which they, according to the Unity documentation, are.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

I think this document from the Unity help should be useful. Look to the diagram at the bottom of the page.

Physics and collisions are processed before the game logic so if you disable an object during collision it will become disabled after its next update.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ That doesn't really confirm it. Destroyed objects aren't actually destroyed until the end of the frame, so it could be the same for disabled ones. \$\endgroup\$ – Ben Nov 19 '14 at 8:30
  • \$\begingroup\$ Disabled objects are actually disabled until the end of the frame, but there's no opportunity for collision detection to occur. However on the flips side the update will still occur so the answer to your question is yes, the update will carry out. \$\endgroup\$ – sydan Nov 19 '14 at 8:32
  • \$\begingroup\$ Edited my response to better answer your question. \$\endgroup\$ – sydan Nov 19 '14 at 8:33
  • \$\begingroup\$ Why would collision detection not be triggered, but the update would? \$\endgroup\$ – Ben Nov 19 '14 at 8:35
  • \$\begingroup\$ Sorry I misunderstood what you're asking, I thought you also wanted to know if collision would continue to occur. (It's early in the morning where I am...). Basically if you disable the object during collision, update will then happen and only after that will the object be disabled. \$\endgroup\$ – sydan Nov 19 '14 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.