- Home /
How do you stop a script executing mid-function?
I have a trigger which uses OnTriggerEnter to start a function in another script. This function starts a countdown using iTween which looks like this:
function Countdown()
{
iTween.ColorTo(gameObject, Color.red, 0.1);
GetComponent(TextMesh).text = "3";
iTween.FadeTo(gameObject, 1, 0.5);
yield WaitForSeconds(1);
iTween.FadeTo(gameObject, 0, 0.5);
yield WaitForSeconds(1);
GetComponent(TextMesh).text = "2";
iTween.FadeTo(gameObject, 1, 0.5);
yield WaitForSeconds(1);
iTween.FadeTo(gameObject, 0, 0.5);
yield WaitForSeconds(1);
GetComponent(TextMesh).text = "1";
iTween.FadeTo(gameObject, 1, 0.5);
yield WaitForSeconds(1);
iTween.FadeTo(gameObject, 0, 0.5);
yield WaitForSeconds(1);
}
However, when I exit the trigger I want the countdown to stop and reset. So I have a reset function which resets the counter:
function ResetCount()
{
iTween.FadeTo(gameObject, 0, 0.1);
GetComponent(TextMesh).text = "3";
}
Now the problem I have is that I need this ResetCount() to "interrupt" the countdown so that when I exit the trigger the countdown stops. However, currently the Countdown() function continues even after the trigger has been exited. I have tried the following in my exit trigger but to no ammends:
function OnTriggerExit(other : Collider)
{
GameObject.Find("Countdown").GetComponent(Countdown).enabled = false;
GameObject.Find("Countdown").GetComponent(Countdown).enabled = true;
iTween.ColorTo(gameObject, Color.black, 0.8);
}
Any ideas on how I can get the Countdown function to stop when the trigger is exited - even if it is in the middle of running?
I don't know if you'd be interested, but many have done coundown timers. Here's one of many: http://forum.unity3d.com/threads/19106-Code-Sample-Countdown-Timer. However, I understand if you want YOUR code, not someone elses. On the OnTriggerExit, why do you make it true again after?
That was just for the next trigger enter - however even when removing that line to make it true again the behaviour is the same.
Thanks for the link. I'll take a look although I would like to figure this out too :D
Alright, I'll test out some things... Post before --:30 hopefully.
Answer by GesterX · Mar 30, 2011 at 11:24 AM
I have a slightly messy workaround but saying that it does get the job done!
if (counting == true)
{
iTween.ColorTo(gameObject, Color.red, 0.1);
GetComponent(TextMesh).text = "3";
iTween.FadeTo(gameObject, 1, 0.5);
yield WaitForSeconds(1);
iTween.FadeTo(gameObject, 0, 0.5);
}
if (counting == true)
{
yield WaitForSeconds(1);
GetComponent(TextMesh).text = "2";
iTween.FadeTo(gameObject, 1, 0.5);
yield WaitForSeconds(1);
iTween.FadeTo(gameObject, 0, 0.5);
}
if (counting == true)
{
yield WaitForSeconds(1);
GetComponent(TextMesh).text = "1";
iTween.FadeTo(gameObject, 1, 0.5);
yield WaitForSeconds(1);
iTween.FadeTo(gameObject, 0, 0.5);
yield WaitForSeconds(1);
}
The variable "counting" is set to true in the TriggerEnter and false in the TriggerExit (I could use the TriggerStay but this also works). So basically I:
- Set the number
- Fade it in
- Fade it out
- Check if we are still counting
- If we are then do the same for the next number - if not then we break out of the code (I also reset the counter in the trigger exit)
Answer by AngryOldMan · Mar 29, 2011 at 11:41 PM
Maybe a daft question but have you tried ontrigger stay?or putting a boolean at the beginning of countdown which turns off when ontrigger exit is called. And this may also be a daft question but I cant understand why you're getting components in the ontrigger exit function? surely it would be in the same script and you could then easily disable it? eg
function OnTriggerEnter() { Countdown(); }
function OnTriggerExit() { ResetCount(); }
function Countdown () { if (CountdownHasBeenCalled == false) { //itween stuff } }
function ResetCount() { CountdownHasBeenCalled = true; }
The problem is even if I do the above the iTween stuff gets called in its entirity since the if check is only called at the start of the code.
The same happens if I use a boolean with the OnTriggerStay function. I have a decent workaround now which I'll post as an answer for people to re-use if they wish. It's a little messy but it get's the job done!
Good for you. Yes please post it as an answer and mark it as correct, helps the community! :)
Answer by Statement · Mar 30, 2011 at 11:53 AM
function OnTriggerEnter() { StartCoroutine("Countdown"); }
function OnTriggerExit() { StopCoroutine("Countdown"); // Alternatively: // StopAllCoroutines(); ResetCount(); }
Answer by tool55 · Mar 30, 2011 at 01:30 PM
function Countdown() {
var countDown : int = 3; iTween.ColorTo(gameObject, Color.red, 0.1);
while (countDown>=0) { GetComponent(TextMesh).text = countDown; iTween.FadeTo(gameObject, 1, 0.5); yield WaitForSeconds(1); countDown--; } }