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 InsaneTameGames · Apr 06, 2016 at 12:09 PM · c#transformlooptranslate

Loop code just stops

Hi all.

I'm trying to make my ground for my game move for 1 minute then get set to inactive but when I get to my second Corrutine it just stops

This is what I've so far done:

using UnityEngine; using System.Collections;

public class GroundMovement : MonoBehaviour {

 private int groundCountdown;
 private int groundMovementCountDown;
 private float movementAmount;

 // Use this for initialization
 void Start () {
     movementAmount = -0.1385f;
     groundCountdown = 60;
     groundMovementCountDown = 100;
      StartCoroutine(BeginningMovement());
 }

 public IEnumerator BeginningMovement()
 {
     transform.Translate(new Vector3(movementAmount, 0.0f, 0.0f));
     yield return new WaitForSeconds(0.001f);
     groundMovementCountDown = groundMovementCountDown - 1;
     if (groundMovementCountDown == 0)
     {
         groundCountdown = groundCountdown - 1;
         transform.position = new Vector3(13.85f, -4.42f, 0.0f);
         groundMovementCountDown = 200;
         StartCoroutine(Movement());
     } else
     {
         StartCoroutine(BeginningMovement());
     }
 }

 public IEnumerator Movement () {
  
     transform.Translate(new Vector3(movementAmount, 0.0f, 0.0f));
     yield return new WaitForSeconds(0.001f);
     groundMovementCountDown = groundMovementCountDown - 1;
     if (groundMovementCountDown == 0)
     {
         groundCountdown = groundCountdown - 1;
         transform.position = new Vector3(13.85f, -4.42f, 0.0f);
     } else
     {
         groundMovementCountDown = 200;
         transform.position = new Vector3(13.85f, -4.42f, 0.0f);
         StartCoroutine(Movement());
     } if (groundCountdown == 0)
     {
         gameObject.SetActive(false);
     }
 }

}

Any suggestions would be greatly appreciated.

Thanks in advance :)

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

2 Replies

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

Answer by Haching · Apr 06, 2016 at 01:22 PM

  if (groundMovementCountDown == 0)
      {
          groundCountdown = groundCountdown - 1;
          transform.position = new Vector3(13.85f, -4.42f, 0.0f);
      } else
      {
          groundMovementCountDown = 200;
          transform.position = new Vector3(13.85f, -4.42f, 0.0f);
          StartCoroutine(Movement());
      }

it seems your code checks if groundMovementCountDown =0, if this is false it sets it to 200, so the code in your if statement will never occur and the loop will continue forever.

remove the part where you set groundMovementCountDown back to 200 and it should work. However theres no need to keep starting your coroutine. Use a while loop and it will be much cleaner. for example something like this:

 public IEnumerator BeginningMovement()
  {
     while (groundMovementCountDown != 0) {
         transform.Translate(new Vector3(movementAmount, 0.0f, 0.0f));
         groundMovementCountDown--;
         yield return new WaitForSeconds(1);        
     }    
  }

So if groundMovementCountDown = 60, then this coroutine will keep moving your GameObject by the distance "movementAmount" in the x direction, once a second for a minute. If I'm not mistaken this will achieve what you want but with considerably less code.

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 Fredex8 · Apr 06, 2016 at 01:48 PM

It isn't a good idea to loop a coroutine by having it call itself again at the end. You should use a for loop within it instead.

Instead of using this:

  yield return new WaitForSeconds(0.001f);
  groundMovementCountDown = groundMovementCountDown - 1;

...and then having the coroutine call itself at the end unless it has reached 0 you should use something like this:

 IEnumerator BeginningMovement()
 {
     for (int i = groundCountdown; i >= 0; i--)
     {
         print(i);
         if (i == 0)
         {
             print("end loop");
             //Do something
         }
         yield return new WaitForSeconds(0.001f);
     }
 }

Rather than waiting for 0.001 seconds 60 times though you are better off using a lower number for groundCountdown and a higher number of seconds to wait.

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

127 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 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

Distribute terrain in zones 3 Answers

How to smooth out movement in a 3D space? 1 Answer

Simple C# Vector2D Moving Question 1 Answer

Move GameObject along a line according to the players look direction 3 Answers

How can I move an object a certain distance, once? 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