Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Jan 21, 2021 at 12:50 PM by DiasPTW for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DiasPTW · Jan 20, 2021 at 01:04 PM · unity 2dgridfor-loopgrid based game

How can i make the for-loop only run a command once

First of all, I'd like to apologize if my English isn't very good but I'll try my best to make it easy to understand. I'm making a 2D game where the player controls some tiles on a grid by using some movement cards that tell where the cards are going to go. I want the card to move 1 unit every x seconds (where to depends on the card), so in order to achieve that I tried using an IEnumerator with a for loop inside (I only put code on the "Up" part to check if everything was working as intended):

 IEnumerator AplicarMovimento()
     {
         for (int i = 0; i < Peças.Length; i++)
         {           
             if (Peças[i] == "Up")
             {
                 transform.position = new Vector2(transform.position.x, transform.position.y + 1);
             }
             else if (Peças[i] == "Down")
             {
                 
             }
             else if (Peças[i] == "Left")
             {
                 
             }
             else if (Peças[i] == "Right")
             {
                 
             }
             yield return new WaitForSeconds(TimeStep);
         }
         canMove = false;
     }

The problem that I have is that the for loop is applying the vector while the TimeStep doesn't reach its destined time, and what I wanted was it applies the vector ONCE and then waits the x seconds (TimeStep), and then it would go into the next "i" in the for-loop. Is there any way to achieve that? Am I missing something? Any help would be appreciated. Thank you for your time!

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 logicandchaos · Jan 20, 2021 at 03:05 PM 1
Share

you can exit a loop using break or return, break will exit the loop and return will exit the function.

1 Reply

  • Sort: 
avatar image
0

Answer by unity_UD6vlOEW25WWeA · Jan 20, 2021 at 04:22 PM

Make sure that you are starting the function using: StartCoroutine("AplicarMovimento");

Comment
Add comment · Show 8 · 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 DiasPTW · Jan 20, 2021 at 04:49 PM 0
Share

Hi, thanks for your answer! Yes I currently call the function on my update function, it's called every time the bool "can$$anonymous$$ove" = true and when the bool "can$$anonymous$$ove" = false it stops the courotine

avatar image Llama_w_2Ls DiasPTW · Jan 20, 2021 at 06:41 PM 0
Share

Did you read the answer that logicandchaos kindly posted?

you can exit a loop using break or return, break will exit the loop and return will exit the function.

@DiasPTW

avatar image Bunny83 DiasPTW · Jan 21, 2021 at 02:36 AM 2
Share

Wait, did you just say that you start a new coroutine every Update? That means you're running several coroutines in parallel. I think your overall concept is flawed. You should start the coroutine once. Coroutines are running "on their own" once started. So I guess since you talk about movement cards that the player is actually arranging, queueing those cards and when done they somehow start the movement sequence. All you have to do is start the coroutine from that point, once. People usually use something like your can$$anonymous$$ove flag to prevent the starting of a new coroutine while one is still running. However for that you would need to set can$$anonymous$$ove to false at the beginning and set it back to true at the end. However since we have no idea how and where you start your coroutine it's pointless to talk about potential fixes as this missing part is most likely where your issue is located.

avatar image DiasPTW Bunny83 · Jan 21, 2021 at 09:00 AM 0
Share

This is currently where the couroutine starts, for now im using keyboard keys but latere on ill be using a button to start the whole thing:

     private void Update()
         {
             if (Input.GetKeyDown(KeyCode.P))
             {
                 can$$anonymous$$ove = true;
             }
             if (Input.GetKeyDown(KeyCode.L))
             {
                 can$$anonymous$$ove = false;
             }
     
             if (can$$anonymous$$ove)
             {
                 StartCoroutine(Aplicar$$anonymous$$ovimento());
             }else { StopCoroutine(Aplicar$$anonymous$$ovimento()); }
         }
     
         IEnumerator Aplicar$$anonymous$$ovimento()
         {
             for (int i = 0; i < Peças.Length; i++)
             {
                 if (Peças[i] == "Up")
                 {
                     transform.position = new Vector2(transform.position.x, transform.position.y + 1);                
                 }
                 else if (Peças[i] == "Down")
                 {
                     
                 }
                 else if (Peças[i] == "Left")
                 {
                     
                 }
                 else if (Peças[i] == "Right")
                 {
                     
                 }
                 yield return new WaitForSeconds(TimeStep);
             }
             can$$anonymous$$ove = false;
         }

I've tried using break and return but those don't really fix the problem

Show more comments
avatar image wolfgraphicsLLC · Jan 21, 2021 at 02:48 AM 1
Share

why not just use a break; after it runs once or could run a while loop?

avatar image wolfgraphicsLLC wolfgraphicsLLC · Jan 25, 2021 at 03:07 PM 0
Share

As you stated here The problem that I have is that the for loop is applying the vector while the Timestep doesn't reach its destined time, the reason this is happening is that it is instantiated in void update which runs and gets called every frame so its not allowing the loop to fully run before it is called again so why not run it in a late or even fixed update?

Follow this Question

Answers Answers and Comments

122 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

Related Questions

!URGENT! How to make a grid of clickable objects. 1 Answer

Grid Letter Box is not expanding, 0 Answers

How to only show objects in range ? 2 Answers

How do I get the object from an adjacent space 2d grid 0 Answers

The 'correct' way to deal with animations in a grid-based game? 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