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 Laurgrin · Mar 09, 2016 at 06:57 AM · scripting beginnereventpause

Pausing script execution until a specific event in another script

I'm trying to come up with a way to pause a script execution until certain conditions are met on another script. Here's the code that I want to pause until that certain event and continue after it is completed.

 void CalculateTurnOrder () {
         units = GameObject.FindGameObjectsWithTag ("Unit").OrderByDescending(go => go.GetComponent<Stats>().speed).ToArray();
         for (int i = 0; i < units.Length; i++) {
             if (i+1 < units.Length) {
                 if (units [i].GetComponent<Stats> ().friendFoe == "Friendly") {
                     units [i].GetComponent<PlayerMoveUnit> ().SelectUnit ();
                 }
                 if (units [i].GetComponent<Stats> ().speed - units [i+1].GetComponent<Stats> ().speed >= units [i+1].GetComponent<Stats> ().speed) {
                     i--;
                 }
             }
         }
     }

The idea is that each unit has a Speed stat, which determines their turn order. What I need is for the iteration, the script itself maybe to stop until the unit, the GameObject you call SelectUnit() on, completes whatever actions it can. THEN the script resumes and most likely gives the next unit in line a move (or move twice if the speed of the i unit is at least twice as big as the i+1 unit).

So how do I make this script wait for a condition like

(!units[i].GetComponent<PlayerMoveUnit> ().IsSelected)

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 Owen-Reynolds · Mar 09, 2016 at 06:46 AM 2
Share

One way is a coroutine. Can make a loop with that skips frames until whenever. But that only works if each units runs itself. This runs all at once? Could rewrite -- just have the loop skip units if it's not their turn.

1 Reply

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

Answer by Laurgrin · Mar 09, 2016 at 05:38 PM

After some digging and not very helpful Unity documentation about WaitUntil, I've accomplished it by adding a few yield lines and turning it into coroutine.

 IEnumerator CalculateTurnOrder () {
         units = GameObject.FindGameObjectsWithTag ("Unit").OrderByDescending(go => go.GetComponent<Stats>().speed).ToArray();
         for (int i = 0; i < units.Length; i++) {
             if (i+1 < units.Length) {
                 if (units [i].GetComponent<Stats> ().friendFoe == "Friendly") {
                     units [i].GetComponent<PlayerMoveUnit> ().SelectUnit ();
                     yield return new WaitUntil (() => !units [i].GetComponent<PlayerMoveUnit> ().IsSelected);
                     yield return new WaitForSeconds (1);
                 }
                 if (units [i].GetComponent<Stats> ().speed - units [i+1].GetComponent<Stats> ().speed >= units [i+1].GetComponent<Stats> ().speed) {
                     i--;
                 }
             }
         }
     }

To anyone else stumbling upon this issue, WaitUntil has some wierd syntax, namely yield return new WaitUntil(() => condition). That honestly should be in the documentation, or I'm just a really green coder for not knowing this specific syntax. Other than that, this issue is solved.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

¿PLAY and Pause with Gear Vr button Video 360? 0 Answers

Don't know how to access UnityEvent listener 1 Answer

Freezing Prefab or deactivating its script in another? 0 Answers

Pausing the game after player gets destroyed 1 Answer

Issues Pausing My App 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