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



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

&nbsp&nbspforeach (Collider c in hits)


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


&nbsp&nbsp    enabled = false;



&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 }


because in both methods you are looping through your hits… but the code “enabled = false” has nothing to do with the hits it returns the same both when inside loop and outside, perhaps u want to do c.gameobject.enabled = false;? anyhow the output should be the same since enabled = false doestn depend on the hits

I’m curious to see how the boolean “enabled” is changing the gameobject this is on. If you have a piece of code like
in an Update function, its gonna be checking for the value in “enabled” every frame. Because your foreach loop is getting called once per frame, it will run its entire course before SetActive is fed the new value of “enabled”. So in one frame, the value of enabled is set to false however many times, but its value isn’t actually CHECKED until the next frame. To get these codes to work how you would expect them to, maybe replace the lines enabled = true and enabled = false with gameObject.SetActive(true) and gameObject.SetActive(false). Just a guess here, but this is all I can think of.