x


Cancel IEnumerator in progress

I have a script that I'm trying to use to track when a player goes out of the map's bounds. If the player doesn't get back in bounds in ten seconds his unit is destroyed. The script works when the player goes out of bounds, but the IEnumerator doesn't stop when he returns to the map.

The bounds is just a box trigger, when the player exits it starts the coroutine, and when he enters I need it to stop the coroutine. Thank you.

void OnTriggerEnter (Collider other) {

    GameObject go = other.gameObject;

    if(go.transform.root.gameObject.CompareTag("Player")){
        status = go.transform.root.gameObject.GetComponent<NetPlayer>();
        status.outOfBounds = false;
    }

}

void OnTriggerExit (Collider other) {

    GameObject go = other.gameObject;

    if(go.transform.root.gameObject.CompareTag("Player")){
        status = go.transform.root.gameObject.GetComponent<NetPlayer>();
        status.outOfBounds = true;
        StartCoroutine(OutOfBounds(10));
    }

}

IEnumerator OutOfBounds(float secs){
    float cd = secs;
    while(status.outOfBounds == true){
        while(cd > 0){
            yield return new WaitForSeconds (1);
            GameObject.Find("MessageText").gameObject.GetComponent<GUIText>().text = "You are leaving the battle.\nReturn to battle or be destroyed.\n" + cd ;
            cd --;
        }
        GameObject.Find("MessageText").gameObject.GetComponent<GUIText>().text = "";
        yield return new WaitForSeconds (.2F);
        status.hullHealth = 0;
    }
}
more ▼

asked Sep 23 '10 at 02:23 PM

mlkielb 1 gravatar image

mlkielb 1
78 13 13 19

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

It's not an IEnumerator. The IEnumerator is the return type of the co-routine. You want to stop the coroutine.

You have the co-routine that does the following:

IEnumerator OutOfBounds(float secs){
    float cd = secs; 
    //Loop conditionals are checked at the end of each iteration.
    //Is there a point in having this?
    //Are you going to kill them multiple times when you don't set outOfBounds = false?
    while(status.outOfBounds == true){
        //Wait secs seconds, posting the message every second
        while(cd > 0){
            yield return new WaitForSeconds (1);
            GameObject.Find("MessageText").gameObject.GetComponent<GUIText>().text =
                "You are leaving the battle.\nReturn to battle or be destroyed.\n" + cd ;
            cd --;
        }
        GameObject.Find("MessageText").gameObject.GetComponent<GUIText>().text = "";
        //Wait .2 seconds and then kill them
        yield return new WaitForSeconds (.2F);
        status.hullHealth = 0;
    }
}

Your co-routine has nothing to stop it until it kills the character and a loop that can do it multiple times. Try to understand what your code is doing as this lack of understanding is the cause of most difficulties.

To do what you're describing, try doing something like:

IEnumerator OutOfBounds(float secs){
    float cd = secs; 
    while(cd > 0) { //Check over secs seconds
        if(!status.outOfBounds) break; //If we're back in bounds, we're done
        yield return new WaitForSeconds (1);
        GameObject.Find("MessageText").gameObject.GetComponent<GUIText>().text =
       "You are leaving the battle.\nReturn to battle or be destroyed.\n" + cd ;
        cd --;
    }
    GameObject.Find("MessageText").gameObject.GetComponent<GUIText>().text = "";
    if(status.outOfBounds) { //We're still out of bounds after secs seconds
        yield return new WaitForSeconds (.2F);
        status.hullHealth = 0;
    }
}
more ▼

answered Sep 23 '10 at 02:50 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

Thank you. All I needed was the break statement. The way the rest of my project is structured it will only kill him once, not multiple times.

Sep 23 '10 at 03:19 PM mlkielb 1

Then why do you have that while loop while(status.outOfBounds == true) if you're only doing one iteration of the loop?

Sep 24 '10 at 02:50 PM skovacs1
(comments are locked)
10|3000 characters needed characters left

You can use StopCoroutine to cancel an existing coroutine, but it must be started using the string version.

more ▼

answered Sep 23 '10 at 03:14 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

(comments are locked)
10|3000 characters needed characters left

Or you use StopAllCoroutines() to kill all on that GO

more ▼

answered Sep 23 '10 at 04:48 PM

Dreamora gravatar image

Dreamora
3.2k 1 4 25

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x324
x66
x19

asked: Sep 23 '10 at 02:23 PM

Seen: 1909 times

Last Updated: Sep 23 '10 at 02:23 PM