• 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 Vice_Versa · Apr 14, 2015 at 09:17 PM · collisioncoroutineoncollisionenteryield waitforseconds

How to end a coroutine early

im working on a whack a mole type game. i have it set up so the moles will pop up and down at randomly generated amounts of time using yieldWaitForSeconds. this works fine but i want the moles to pop back down if they collide with the users mallot. the game notices the collisioins, but the moles still stay up for the remaining amount of time. here is my code using UnityEngine; using System.Collections;

 public class tester : MonoBehaviour {
 
 
 
     bool isMoving = true; 
     bool moleUp = false;
     bool isColliding = false;
     public AudioClip moleUpSound;
     public AudioClip moleDownSound;
 
     float X;
     float Y;
     float Z;
     // Use this for initialization
 
     void Start()
     {
         StartCoroutine ("moleMove");
     }
         void Update()
         {
 
         if (this.gameObject.name == "Mole_1") {
             X = -1F;
             Y = 1.5F;
             Z = -1.6F;
         } else if (this.gameObject.name == "Mole_2") {
             X = 4F;
             Y = 1.5F;
             Z = -1.6F;
         } else if (this.gameObject.name == "Mole_3") {
             X = 9F;
             Y = 1.5F;
             Z = -1.6F;
         } else if (this.gameObject.name == "Mole_4") {
             X = -1F;
             Y = 1.5F;
             Z = -6.6F;
         } else if (this.gameObject.name == "Mole_5") {
             X = 4F;
             Y = 1.5F;
             Z = -6.6F;
         } else if (this.gameObject.name == "Mole_6") {
             X = 9F;
             Y = 1.5F;
             Z = -6.6F;
         }
 
 
 
         if (moleUp == false) {
             if (Y < 3F) {
 
                 moveUp();
 
             }
         }
 
         if (moleUp == true) {
 
             if (Y > 1F) {
         
                     moveDown();
 
 
             }
         }
     }
     IEnumerator moleMove ()
     {
         int i;
 
         for (i = 1; i > 0; i++) {
             if (moleUp) {
                 PlaySound ();
                 moleUp = false;
                 float generator = Random.Range (2, 3);
             
                 yield return new WaitForSeconds(generator);
 
             } else if (moleUp == false) {
                 PlaySound ();
                 moleUp = true;
                 float generator = Random.Range (2, 6);
             
                 yield return new WaitForSeconds (generator);
             }
         
             }
         }
 
 
     public void PlaySound()
     {
         if (moleUp) {
             audio.clip = moleUpSound;
 
             audio.Play ();
         }
         if (!moleUp) {
             audio.clip = moleDownSound;
             audio.Play ();
         }
 
 
     }
 
     public void OnCollisionEnter(Collision col)
     {
         Vector3 newPosition = new Vector3 (X, 3.5F, Z);
         Vector3 oldPosition = new Vector3 (X, -1F, Z);
         Debug.Log ("Collision!");
         isColliding = true;
         moleUp = false;
 
         moveDown ();
     }
 
     public void moveUp()
     {
         if (isColliding == true) {
             isColliding = false;
         }
         Vector3 newPosition = new Vector3 (X, 3.5F, Z);
         iTween.MoveTo (gameObject, iTween.Hash ("Position", newPosition, "time", 1, "transition", "spring"));
         
     }
 
     public void moveDown()
     {
 
     
         Vector3 oldPosition = new Vector3 (X, -1F, Z);
         iTween.MoveTo (gameObject, iTween.Hash ("Position", oldPosition, "time", 1));
     
     }
 }
 

i would think my moveDown() method in the onCollisionEnter method would move the mole back down, but instead it just bobs around until the remaining time from moleMove runs out. does anybody know how to fix this problem?

Comment
Add comment · Show 1
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 maccabbe · Apr 14, 2015 at 09:18 PM 0
Share

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StopCoroutine.html

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Changing transform.localScale but OnCollisionEnter isn't executed 1 Answer

Help with collisions and destroy please 1 Answer

Play sound on collision doesn't work 1 Answer

Another Collision Question 3 Answers

OnCollision Works for one script but not the other? 2 Answers


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