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 JamesEvanNeal · May 17, 2017 at 12:04 PM · listcoroutinesorder-of-executioncommands

A list of commands that will wait before running the next

I have a list that is filled randomly with vector3s and a time to go with them. I want to be able to run each element in order but only if the previous has been run. I am curious if any one has programmed something along these lines before or has a good approach.

         Vector3 first = transform.up;
         float time = 3.0f;
         
         Vector3 second = transform.up;
         float time2 = 5.0f;
         
         Vector3 third = transform.up;
         float time3 = 8.0f;
         
         applyDirection(first,time);
         wait and see if first is complete
         if true
         applyDirection(second,time2);
         else
         nothing











Comment
Add comment · Show 3
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 SohailBukhari · May 17, 2017 at 12:28 PM 0
Share

use WaitUntil,WaitUntil Suspends the coroutine execution until the supplied delegate evaluates to true.

WaitUntil can only be used with a yield statement in coroutines.

Supplied delegate will be executed each frame after script $$anonymous$$onoBehaviour. Update and before $$anonymous$$onoBehaviour.LateUpdate. When the delegate finally evaluates to true, the coroutine will proceed with its execution.

Example

 public class WaitUntilExample : $$anonymous$$onoBehaviour
 {
     public int frame;
 
     void Start()
     {
         StartCoroutine(Example());
     }
 
     IEnumerator Example()
     {
         Debug.Log("Waiting for princess to be rescued...");
         yield return new WaitUntil(() => frame >= 10);
         Debug.Log("Princess was rescued!");
     }
 
     void Update()
     {
         if (frame <= 10)
         {
             Debug.Log("Frame: " + frame);
             frame++;
         }
     }
 }

avatar image AlwaysSunny · May 17, 2017 at 12:30 PM 0
Share

@SohailBukhari shows a valid way to handle this task using coroutines. I was writing my comment at the same time. :)

The code you shared is not a list, but I'm going to assume you're using an actual generic List of Vector3's and a List of times. If you're not, it sounds like you probably should be.

The easiest way would involve removing each List element as it's used. If you can't remove them for some reason, you could keep an integer index as a record of the last element used. If that won't work for some reason, you could create a duplicate version of the original list and remove elements from that. If that won't work, we'll talk. There's always a nice way to model simple behaviors like this.

If you did not mean a generic list, you shouldn't use words like list or element. In that case, perhaps the best way involves creating a new kind of object. (Simply because managing a pair or group of related lists can be messy if you're not careful.)

Your new class could be something like a $$anonymous$$oveOrder, which has a Vector3 direction, a float timeToBegin (or timeUntilComplete, whatever), and a bool hasCompleted. Then you could have a list of these objects, and refer to the paragraph above for making use of them.

avatar image JamesEvanNeal AlwaysSunny · May 19, 2017 at 03:57 AM 0
Share

Thank you for your help :)

I ended up removing elements as they were used. much appreciated.

2 Replies

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

Answer by Shrikky23 · May 17, 2017 at 11:19 PM

 public bool canProceed = false;
 public IEnumerator void WaitForTimer(float X)
 {
   int timer = 0;
   canProceed = false;
   while (timer < X)
   {
     yield return null;
   }
   canProceed= true;
   Debug.Log( " Wait for X number of seconds, You can now Proceed";
 }

}

Use Coroutines to create timers and do your calculation. If you want to learn something new, Learn Promise timers http://www.what-could-possibly-go-wrong.com/promises-for-game-development/

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 JamesEvanNeal · May 19, 2017 at 03:16 AM 0
Share

Thanks! The example you gave was a simple version of what I ended up with. Yours helped a lot. Also the resource you gave for promises was a really great read. I will definately look into using them for future problems.

avatar image
0

Answer by toddisarockstar · May 17, 2017 at 09:47 PM

     int idx;
     Vector3[] v;
     float[] f;
 
 
     void Start () {
         v = new Vector3[3];
         f = new float[3];
 
         v [0] = new Vector3 (1f, 1f, 1f);
         v [1] = new Vector3 (2f, 2f, 2f);
         v [2] = new Vector3 (3f, 3f, 3f);
 
         f [0] = 1f;
         f [1] = 2f;
         f [2] = 3f;
 }
     void Update(){
         if(Input.GetKeyDown("space")){doit();}
     }
 
     void doit () {if (idx < v.Length) {
                         print ("vector is :" + v [idx] + "float is: " + f [idx]);
                         idx++;
                         } 
         else {print ("im all done doing it");}
     
     }
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

70 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

Related Questions

A node in a childnode? 1 Answer

Java Script command list 3 Answers

Switching Target during Target Lock 1 Answer

Best way to add item to other script list? 0 Answers

How to draw text based on text above it? 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