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
0
Question by awplays49 · Mar 30, 2015 at 09:39 PM · coroutineienumerator

Coroutine WaitForSeconds is varying? How to fix?

I know this code is long, so the IEnumerator is at the bottom, and the initiation is at the top of update. My code is in an attempt to, while holding a move button, move in that direction every half second. Its speeding up, meaning it's varying.

EDIT

I cut the code down so its only important stuff :-)

EDIT 2

Also, when i cut the code, i cut things in between and even though i separated parts, it kept them as one piece of code. using UnityEngine; using UnityEngine.UI; using System.Collections;

 public class Cube : MonoBehaviour {
     
     public GameObject [] ManagerGet = new GameObject [2];
     public GameObject AddButtonGet;
     public Text TextGet;
     public int DataValue;
     public bool CanSubtract;
     public bool IsSelectable;
     public bool IsMoving;
 
     public enum Types {
         Cube,
         Tree
     };
     public Types Type;
 
     void Update () {
         if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D))
         {
             if (IsMoving == false)
             {
                 StartCoroutine (Move ());
                 IsMoving = true;
             }
         }
         else
         {
             StopCoroutine (Move ());
             IsMoving = false;
         }
 
     IEnumerator Move () {
         if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
         {
             if (Input.GetKey (KeyCode.W))
             {
                 if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
                 {
                     transform.position += Vector3.up;
                 }
                 if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.y >= Camera.main.transform.position.y + 4)
                 {
                     Camera.main.transform.position += Vector3.up;
                 }
                 if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
                 {
                     Instantiate (gameObject, transform.position, Quaternion.identity);
                 }
             }
             if (Input.GetKey (KeyCode.A))
             {
                 if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
                 {
                     transform.position += Vector3.left;
                 }
                 if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.x <= Camera.main.transform.position.x - 5)
                 {
                     Camera.main.transform.position += Vector3.left;
                 }
                 if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
                 {
                     Instantiate (gameObject, transform.position, Quaternion.identity);
                 }
             }
             if (Input.GetKey (KeyCode.S))
             {
                 if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
                 {
                     transform.position += Vector3.down;
                 }
                 if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.y <= Camera.main.transform.position.y - 4)
                 {
                     Camera.main.transform.position += Vector3.down;
                 }
                 if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
                 {
                     Instantiate (gameObject, transform.position, Quaternion.identity);
                 }
             }
             if (Input.GetKey (KeyCode.D))
             {
                 if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
                 {
                     transform.position += Vector3.right;
                 }
                 if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.x >= Camera.main.transform.position.x + 5)
                 {
                     Camera.main.transform.position += Vector3.right;
                 }
                 if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
                 {
                     Instantiate (gameObject, transform.position, Quaternion.identity);
                 }
             }
         }
         yield return new WaitForSeconds (0.5f);
         Debug.Log ("YES");
         StartCoroutine (Move ());
     }
 }
 
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Addyarb · Mar 30, 2015 at 11:20 PM

I've had similar issues. While I'm not an expert with IEnumerators, something like this usually helps: This is usually known as condition validation.

  IEnumerator MyCoRoutine(){
     if(!yielding){
     yield return new WaitForSeconds(secondsToWait);
     yielding = false;
     }
     }
 

Edit: Basically what I'm getting at is that you are probably starting the coroutine multiple times, and that is giving you unpredictable behavior. It is better to only run the coroutine once and wait for the bool to "expire" before beginning a new one.

Comment
Add comment · Show 13 · 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 awplays49 · Mar 30, 2015 at 11:27 PM 0
Share

Do you know what i am doing to make it repeat too fast?

avatar image Addyarb · Mar 30, 2015 at 11:40 PM 0
Share

Well it's sort of hard for me to explain.

$$anonymous$$y guess to what is happening internally is that since you're using a non-conditional initializer (your Input.Get$$anonymous$$ey) that you are calling the coroutine over and over. This sets of a series of instances of that coroutine that will appear to make things seem to happen faster since you actually have many coroutines happening ins$$anonymous$$d of just one.

The solution (if this is the problem) would be to set a bool true whenever the coroutine is running, and set it false as soon as it's done running. That way you know only one coroutine is running.

Another way to check to see what's going on it to put something like this in your coroutine:

 yield return new WaitForSeconds(1);
 Debug.log("Waited 1 second");
 yield return new WaitForSeconds(1);
 Debug.log("Waited 2 seconds");
 yield return new WaitForSeconds(1);
 Debug.log("Waited 3 seconds");
 yield return new WaitForSeconds(1);
 Debug.log("Waited 4 seconds");
 yield return new WaitForSeconds(1);
 Debug.log("Waited 5 seconds");
 
 

Obviously if you see multiple, identical, or out-of-order messages, then you are running many coroutines.

avatar image awplays49 · Mar 30, 2015 at 11:46 PM 0
Share

no because i put the condition is moving = false in there. and after its hit, is moving = true.

avatar image Addyarb · Mar 30, 2015 at 11:49 PM 0
Share

You are right. The only way I can get it to work is by changing StopCoroutine($$anonymous$$ove()); to StopAllCoroutines();

avatar image awplays49 · Mar 30, 2015 at 11:50 PM 0
Share

Hmm why is that do you think? im not being sarcastic im curious too :P

Show more comments
avatar image
0

Answer by ian_sorbello · Mar 31, 2015 at 01:25 AM

Calling StartCoroutine( ) within the coroutine itself isn't good. Also, calling StartCoroutine(Move()) in the Update loop is the reason why you've got re-entrency issues. You're starting a Move() function call every frame, so it's definitely the reason for your problem.

Change the CoRoutine to this:

 IEnumerator Move () {
   for(;;) {
      // Do all of your input checking here
      // ...
      // Then wait
      yield return new WaitForSeconds(0.5f);
   }
 }

Note: You only start the coroutine once, and the for(;;) keeps the coroutine going forever, you don't need to start it again - there's only one loop in play- and your timings will be accurate.

Therefore, don't call StartCoroutine from the Update() loop - start it up only once on Awake() or Start().

You might need to change the logic about checking if you're moving etc. within the Coroutine itself.

EDIT: I wrote the code for how this would work. I couldn't check if it compiled, as I don't have your classes to compile against - but here's how it should work:

 using UnityEngine;
 using System.Collections;
 
 
 public class Cube : MonoBehaviour {
         
     public GameObject [] ManagerGet = new GameObject [2];
     public GameObject AddButtonGet;
     public Text TextGet;
     public int DataValue;
     public bool CanSubtract;
     public bool IsSelectable;
     public bool IsMoving;
     
     public enum Types {
         Cube,
         Tree
     };
     public Types Type;
 
     void Start() {
         // Just start the coroutine once
         StartCoroutine (Move());
     }
 
 
     void Update () {
         // Do something else here - the coroutine is looking after itself
     }
 
     IEnumerator Move () {
         for(;;) {
             if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
             {
                 if (Input.GetKey (KeyCode.W))
                 {
                     IsMoving = true;
 
                     if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
                     {
                         transform.position += Vector3.up;
                     }
                     if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.y >= Camera.main.transform.position.y + 4)
                     {
                         Camera.main.transform.position += Vector3.up;
                     }
                     if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
                     {
                         Instantiate (gameObject, transform.position, Quaternion.identity);
                     }
                 }
                 if (Input.GetKey (KeyCode.A))
                 {
                     IsMoving = true;
 
                     if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
                     {
                         transform.position += Vector3.left;
                     }
                     if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.x <= Camera.main.transform.position.x - 5)
                     {
                         Camera.main.transform.position += Vector3.left;
                     }
                     if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
                     {
                         Instantiate (gameObject, transform.position, Quaternion.identity);
                     }
                 }
                 if (Input.GetKey (KeyCode.S))
                 {
                     IsMoving = true;
 
                     if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
                     {
                         transform.position += Vector3.down;
                     }
                     if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.y <= Camera.main.transform.position.y - 4)
                     {
                         Camera.main.transform.position += Vector3.down;
                     }
                     if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
                     {
                         Instantiate (gameObject, transform.position, Quaternion.identity);
                     }
                 }
                 if (Input.GetKey (KeyCode.D))
                 {
                     IsMoving = true;
 
                     if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
                     {
                         transform.position += Vector3.right;
                     }
                     if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.x >= Camera.main.transform.position.x + 5)
                     {
                         Camera.main.transform.position += Vector3.right;
                     }
                     if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
                     {
                         Instantiate (gameObject, transform.position, Quaternion.identity);
                     }
                 }
             }
 
             // Wait before we loop again
             yield return new WaitForSeconds (0.5f);
             Debug.Log ("YES");
         }
     }
 }
 

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 eelstork · Mar 31, 2015 at 02:25 AM

I'm with Ian here, in that sorry I'm not bothered figuring exactly what you're doing with your cubes (instantiating cubes along a path determined by keyboard input?) but here is a script that lets you move a cube using the keyboard, factoring in your delay constraint.

 using UnityEngine;
 using System.Collections;
 
 public class SteppedMotionController : MonoBehaviour {
 
 void OnEnable() { StartCoroutine ("Move"); }

 private IEnumerator Move(){

     while (enabled) yield return StartCoroutine("Step");

 }

 private IEnumerator Step(){

     Vector3 u = Vector3.zero;

     // Wait for input

     while (u==Vector3.zero) {

         if (Input.GetKey (KeyCode.W)) u.y = 1.0f;
         if (Input.GetKey (KeyCode.A)) u.x = -1.0f;
         if (Input.GetKey (KeyCode.S)) u.y = -1.0f;
         if (Input.GetKey (KeyCode.D)) u.x = 1.0f;

         yield return null;

     }

     // Move

     transform.position += u;

     // Wait for cooldown

     yield return new WaitForSeconds (1.0f);

 }

 }

Although simplified, the advantage is that since I removed your dependencies to external stuff I could test this to make sure it actually works. As you can see the idea is the same as what Ian suggested, where you run a "Step" function which does three things in a sequence:

  1. Wait for input

  2. Move the object

  3. Wait for cooldown.

There are other ways to do this but in your case this may be easier and simpler.

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

22 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

Related Questions

spawn timer problem 2 Answers

making a 2D character Speak for different lenghts of time 2 Answers

Why do you use Coroutines and how do you use them? 1 Answer

[ Coroutine: Move Next ] CPU usage 0 Answers

Cancel IEnumerator in progress 3 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