why do these both codes work similarly when they are put inside update function...

Method1::

{

&nbsp&nbspCollider hits = Physics.OverlapSphere(transform.position, radius, enemyLayer);

&nbsp&nbspforeach (Collider c in hits)

&nbsp&nbsp{

&nbsp&nbsp&nbsp&nbsp c.gameObject.GetComponent().TakeDamage(damageAmount);

&nbsp&nbsp}

&nbsp&nbsp    enabled = false;

}

Method2::

{
&nbsp&nbsp Collider hits = Physics.OverlapSphere(transform.position, radius, enemyLayer);

  &nbsp&nbsp  foreach (Collider c in hits)

  &nbsp&nbsp  {

&nbsp&nbsp&nbsp&nbsp        c.gameObject.GetComponent<EnemyHealth>().TakeDamage(damageAmount); 

 &nbsp&nbsp&nbsp&nbsp        enabled = false;

   &nbsp&nbsp }

}

Well changing the enabled state of your script does not terminate the execution of your method. Actually the only thing that could terminate the execution would be an exception. If no exception is thrown a method will always execute until it’s finished. This is even true when you deactivate or destroy your gameobject. Note when i said “destroy” i even mean DestroyImmediate and not Destroy as Destroy is always delayed until the end of the current frame (which includes the end of your method ^^).

Have a look at this example:

void Update ()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        DestroyImmediate(gameObject);
        Debug.Log("After destroyed gameObject");
        Debug.Log("Destroyed: " + name);
        Debug.Log("This will not print");
    }
    Debug.Log("Finished Update");
}

When you press the Space bar the if condition will be true and the Update method will enter the if body. The first debug.Log statement is still executed because as i said the current method will continue until it reaches the end or if an exception is thrown. In this case here we will get an exception when the “name” property is evaluated. This is because the name of the gameobject was stored on the native C++ side. However when destroying an object the native object doesn’t exist anymore. Though the managed object (your actual script instance) still exists since managed objects can never be destroyed manually. As long as your script does not touch anything related to the native object (like calling GetComponent, accessing “transform”, etc on your destroyed object) the method will always complete.

In your case you only set the enabled state of your script to false. That means the object is still there and only the next frame Unity will not call Update on your object. However the current execution is of course finished.

Because that enable is generic, it may be a global and will always set to false

Note: I am partly working off of your comment on @hectorux’s answer


No because to disable a game object you need to do GameObject.SetActive(false);. enabled doesn’t have control of this. If you want the change in enabled to immediately change the state of the game object than you have to do this:

{    Collider[] hits = Physics.OverlapSphere(transform.position, radius, enemyLayer);
   foreach (Collider c in hits)
   {
     c.gameObject.GetComponent().TakeDamage(damageAmount);
     enabled = false;
     c.gameObject.SetActive(enabled); //This is assuming that you are trying to disable c.gameObject
   }
}

So essentially, the reason why they work the same way it seems (I can’t tell definitively without more code) is because you are using c.gameObject.SetActive(enabled) somewhere later in the code. enabled will be set to false however it will only change the state of the game object once you have called the SetActive method.