Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by GesterX · Mar 29, 2011 at 09:11 PM · functionitweeninterrupt

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Justin Warner · Mar 29, 2011 at 09:24 PM 1
Share

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?

avatar image GesterX · Mar 29, 2011 at 09:30 PM 0
Share

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

avatar image Justin Warner · Mar 29, 2011 at 10:09 PM 0
Share

Alright, I'll test out some things... Post before --:30 hopefully.

avatar image Justin Warner · Mar 29, 2011 at 10:28 PM 0
Share

Nothing's working for me right now, sorry =/

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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:

  1. Set the number
  2. Fade it in
  3. Fade it out
  4. Check if we are still counting
  5. 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)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GesterX · Mar 30, 2011 at 11:19 AM 0
Share

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!

avatar image AngryOldMan · Mar 30, 2011 at 11:21 AM 0
Share

Good for you. Yes please post it as an answer and mark it as correct, helps the community! :)

avatar image
0

Answer by Statement · Mar 30, 2011 at 11:53 AM

function OnTriggerEnter() { StartCoroutine("Countdown"); }

function OnTriggerExit() { StopCoroutine("Countdown"); // Alternatively: // StopAllCoroutines(); ResetCount(); }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to create a bouncing effect 1 Answer

iTween oncomplete not firing? 1 Answer

Function after iTween animation finished 1 Answer

iTween - calling functions on oncomplete doesn't work if the function is declared as a variable 1 Answer

onMouseDown call function from other script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges