Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by dotcommer · Aug 09, 2017 at 03:38 AM · buttoncoroutineanimationcurve

Coroutine stops working after first run

Premise:

I've got a button (made of a quad) working properly that references an animation curve to animate when clicked.

Issue

I can click my button once, and it'll run through the coroutine that uses the animation curve to animate it. But after that, I can't get it to trigger again. I know its still receiving the click events from the log, but the coroutine won't run after the first time its run. Any ideas why? Code for the button script below:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Button : MonoBehaviour {
 
 
     public AnimationCurve curveAnim;
     public float posDisplacement;
     public float speed;
 
     public void KickBack(){
         StartCoroutine (kickback());
     }
 
     IEnumerator kickback(){  //animation coroutine
         float curveTime = 0f;
         float curveAmount = curveAnim.Evaluate (curveTime);
 
         while (curveAmount < 1.0f) {
             curveTime += Time.deltaTime * speed;
             curveAmount = curveAnim.Evaluate (curveTime);
             transform.position = new Vector3 (transform.position.x, transform.position.y, curveAmount*posDisplacement);
             yield return null;
         }
     }
 
     //Button events called from "gameObject.SendMessage." in another script
     void OnTouchDown(){
         Debug.Log ("Touch Down!");
     }
 
     void OnTouchUp(){
         Debug.Log ("Touch Up!");
         KickBack ();
     }
 
     void OnTouchStay(){
         
     }
 
     void OnTouchExit(){
 
     }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by FlaSh-G · Aug 11, 2017 at 08:50 PM

It's just a guess because the code looks mostly fine, but I think your first Coroutine never stops and happens to be executed after all following Coroutine calls forever after.

The coroutine ends when

 curveAmount < 1.0f

is the case, and depending on how you set your AnimationCurve, this might be true forever.

I suppose you initially wanted to check

 curveTime < 1.0f

anyway, which would be reliable to go over 1 sooner or later (unless speed is less than or equal to 0).

Comment
Add comment · Show 1 · 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 dotcommer · Aug 12, 2017 at 05:50 PM 0
Share

Yup! Thats exactly what it was. I was referencing the amount ins$$anonymous$$d of time. Thanks!

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

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a coroutine to wait for a button press? 1 Answer

Wait for button response in coroutine? 2 Answers

Is it possible to start a coroutine using a button from the new UI system (4.6) 1 Answer

AnimationCurve as fixed time rather than portion of time? 1 Answer

I am trying to take away some health from a slider using a coroutine, but it takes away too much 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