- Home /
[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");
}
Answer by DaveA · Aug 01, 2013 at 02:07 AM
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.
hmm... The Unity engine keeps freezing each time my player exits the trigger area. Not sure if that's my computer acting up, or if there's a problem with the script..
Here's my updated script:
public static var hitPoints :int = 100.0;
var pause = false;
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; ){
if (!pause)
{
yield WaitForSeconds (1);
hitPoints -=1;
i ++;
}
}
}
}
function OnTriggerExit (other: Collider){
//boxHealth.SetActive (false);
boxHealth.GetComponent(GUIText).enabled = false;
pause = true;
Debug.Log("You've Exited my Trigger");
}
try
for (var i=0; i<100 && !pause ; ){
yield WaitForSeconds (1);
hitPoints -=1;
i ++;
}
Right now, if pause is true, your indexer won't increase and you're stuck in an infinite for loop. Alternatively you could move i++; between the two curly backets;
Perfect! Thanks, its working now.
You guys have been a huge help :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613111319im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Turret Script Error 2 Answers
OnTriggerEnter function in c# 0 Answers
Make object react to certain triggers only 1 Answer
Box collider with strange behaviour 1 Answer
Help with OnTriggerEnter issue 3 Answers