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 PandawanFr · Jan 18, 2016 at 10:07 AM · coroutinewaitmethodsafter

Make a wait function

Hello, so I am trying to automate my system a little bit, basically I am trying to make it so I declare everything that I want to happen line after line, and then my code saves all this into a List and runs them one after the other. Now that's good and all, but I have a small problem, when I want the game to just wait a certain amount of time (because I want cinematics to play while this wait function plays or something else).

So I am using coroutines of course, and after running my wait function, nothing happens, it doesn't run the next line, so please help?

Thanks!

here is my code:

 public bool isDone = true;
 
     List<string> methodList = new List<string>();
 
     void Start ()
     {
         AddNext("test", new string[] { "hey" });
         AddNext("wait", new string[] { "10" });
         AddNext("test", new string[] {"hey"});
     }
 
     void AddNext (string method, string[] args)
     {
         string temp = method + "_" + string.Join("_", args); ;
         methodList.Add(temp);
         StartCoroutine(AddNextNow());
     }
 
     IEnumerator AddNextNow()
     {
         string methodNow = methodList[0];
 
         if (isDone)
         {
             isDone = false;
             string[] all = methodList[0].Split('_');
 
             if (all[0] == "test")
             {
                 Test(all[1]);
             }
             else if (all[0] == "wait")
             {
                 yield return StartCoroutine_Auto(Wait(float.Parse(all[1])));
             }
 
             methodList.Remove(methodList[0]);
             isDone = true;
             if (methodList[0] != null) {
                 StartCoroutine(AddNextNow());
             }
         }
     }
 
     void Test (string input)
     {
         Debug.Log("Test! " + input);
     }
 
     IEnumerator Wait (float delay)
     {
         Debug.Log("Waiting...");
         yield return new WaitForSeconds(delay);
         Debug.Log(methodList[0]);
     }
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 Fattie · Jan 19, 2016 at 07:35 PM 0
Share

in general just use Invoke for timers in Unity, Is there a chance you could google for duplicates of this question first - you'll find many good answers here!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RoofTurkey · Jan 18, 2016 at 10:16 AM

Why wouldn't you just use

 yield return new WaitForSeconds(delay);

Instead of an entire function for this?

As far as I know a coroutine doesn't wait before it's finished, so it won't hold back other code.

Edit: Perhaps it's worthwhile to read this: http://answers.unity3d.com/questions/276055/question-about-coroutines-and-waiting-for-a-functi.html

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 Bonfire-Boy · Jan 18, 2016 at 10:59 AM 0
Share

When a coroutine is started within another, using yield return StartCoroutine, the outer CR does "hold back other code" (wait for the inner one to finish). That's standard.

Wrapping the WaitForSeconds call in another CR does look superfluous here, especially given that it's only used in one place (if it were used in multiple places then the wrapping function would be perfor$$anonymous$$g a potentially useful task in that it enables the logging code to be added without repetition). But that doesn't look like the problem to me.

I'm not sure what the problem is, $$anonymous$$d. The code confuses me, especially the way that 3 versions of the AddNextNow CR are started in the same frame (the Start function), and they share the isDone flag, and also each kick off the CR again at the end.

I suspect that AddNext() should not be starting the CR at all. Ins$$anonymous$$d you'd start it at the end of the Start function, once methodList has been populated - that way you'd only have one AddNextNow running at a time. But I'd probably just start over, using a flowchart.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My Coroutine is freezing my game? (no loops, serverSocket.Connect) 1 Answer

I want to create a coroutine that will run a series of functions when a set variable reaches its value. What would be the best way to do this? 0 Answers

Sequentially wait for multiple coroutines to finish 0 Answers

WaitUntil and WaitWhile don't work 1 Answer

Hold or Wait while Coroutine finishes 6 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