[Solved] Trying to pause a for loop function OnTriggerExit

I am fairly new to scripting, and am trying to learn as much as possible about the Unity engine. Currently, I am having trouble with the concept of the “For” loop. Currently, I have an Enemy Object with a trigger radius and a “hitPoints” var attached to it set to 100. I have a for loop that OnTriggerEnter, the hitPoints variable goes down with every second.

The problem is, OnTriggerExit, I need that loop to pause and then resume again OnTriggerEnter at the same place it left off, but I don’t know where to start… Can you help me please?

Also, (this is a secondary problem)
I have a “destroyEnemy1()” function that if the variable hitPoints hit zero, the gameobject is destroyed, but its not executing properly. The Enemy1’s “hitPoints” variable hits zero, then just sits there. If my “Player” object shoots bullets at it, the variable goes down into the negatives…


Here is the code currently attached to the Enemy1 object:

(I know its sloppy and contains many unnecessary variables and code. I created them while experimenting with code i’ve deleted most of the code unrelated to my question. If you need anything else or if i’m not being specific enough, just let me know)

public static var myLog : String;
public static var hitPoints :int = 100.0;
var youWin : GameObject;
public static var some_Name : String = "Enemy BOSS ";
var boxHealth : GameObject;
internal var forTime : float;

function OnCollisionEnter(other: Collision) {

if (other.gameObject.tag == "Bullet") {
	boxHealth.GetComponent(GUIText).enabled = true;
	hitPoints -=1;
}

}

function destroyEnemy1() {

if (hitPoints <=0) {
	Destroy (gameObject);
	print("i'm Dead");
	
	GUI.enabled = false;
	Instantiate (youWin, transform.position, transform.rotation);
}

	else{ 
		print("i'm still alive");
		
		//boxHealth.SetActive (true);
	}

}
function OnTriggerEnter (other: Collider) {
	if (other.tag == "Player"){
 	Debug.Log("youve Entered my trigger");
 	boxHealth.GetComponent(GUIText).enabled = true;
	
	for (var i=0; i<100; i++){
	yield WaitForSeconds (1);
	 hitPoints -=1; 
	}
}
}

function OnTriggerExit (other: Collider){

	//boxHealth.SetActive (false);
	boxHealth.GetComponent(GUIText).enabled = false;
	//pause for loop action script goes here
	Debug.Log("You've Exited my Trigger");
}

You can declare a variable at the top of your script like:

var pause = false;

then in ‘exit’ set pause = true;

then in the for loop, just test for it.

  for (var i=0; i<100; ){
    if (!pause)
    {
    yield WaitForSeconds (1);
     hitPoints -=1; 
     i ++;
    }
    }

But it’s getting convoluted. I personally would be using the Update function instead of a for loop.