# When are Unity scripts actually disabled?

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?

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.

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.

• 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. – Ben Nov 19 '14 at 8:30
• 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. – sydan Nov 19 '14 at 8:32
• Edited my response to better answer your question. – sydan Nov 19 '14 at 8:33
• Why would collision detection not be triggered, but the update would? – Ben Nov 19 '14 at 8:35
• 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. – sydan Nov 19 '14 at 8:36