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 /
  • Help Room /
avatar image
0
Question by Jason2-0 · Dec 04, 2015 at 03:56 PM · loopnoobcoroutine errors

Problem with nested Start Coroutine in loop

Hi, this question probably has a simple solution but I cant seem to find it in the Unity documentation or in past problems. Im trying to get a platform to move up 30 spaces along y axis wait 5 seconds, move down 30 spaces along y axis and then repeat this for the duration of the game. For some reason the platform either moves only up, only down or moves up once and then stops. Any help would be greatly appreciated. The code I use has been attached.

unity-problem.png (24.8 kB)
Comment
Add comment · Show 2
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 gjf · Dec 03, 2015 at 05:07 PM 0
Share

please paste your actual code not screenshots of it. sometimes it's necessary to use the code in order to help you, and it's unlikely that anyone will type in in... the user guide/faq (linked from the right rail) explain how to...

avatar image Jason2-0 · Dec 04, 2015 at 08:44 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class LiftController : $$anonymous$$onoBehaviour {
     void Start () {
     //    for (int i = 0; i > 10; i ++) {
         //    StartCoroutine (Wait ());
         //}
     }
     void Awake(){
         StartCoroutine ("Wait");
     }
     void Update () {
         //StartCoroutine (Wait ());
     }
 
     IEnumerator Wait(){
         while (true) {
             yield return new WaitForSeconds(5);
             transform.Translate (new Vector3(0, 30, 0));
             StartCoroutine (Wait2 ());
         }
 
     }
     IEnumerator Wait2(){
         yield return new WaitForSeconds (5);
         transform.Translate (new Vector3(0, -30, 0));
     }
 
 }

1 Reply

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

Answer by pekalicious · Dec 04, 2015 at 06:51 PM

You are calling Wait2 without yielding, which means that it will start the coroutine but not wait for it to complete, so it will immediately go to the first WaitForSeconds. And because both coroutines wait 5 seconds, they will execute the transform logic at the same time, resulting in unpredictable behavior.

Change

 StartCoroutine (Wait2 ());

To

 yield return StartCoroutine (Wait2 ());
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 Jason2-0 · Dec 05, 2015 at 08:39 AM 0
Share

Ok thanks pekalicious, that solved it. Unfortunately I lost most of the game with the new update but my scripts were still saved, so i'll keep them for future reference. :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why isn't my coroutine working when I call it from another script. 0 Answers

How do i make Unity seamlessly loop my background music? 5 Answers

Video not playing completely? 0 Answers

For loop work well except on third state 1 Answer

Trigger a certain number of loop in animation on button down 0 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