Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
5
Question by awdawdawd · Aug 19, 2012 at 01:56 PM · variableyieldwaitcontinue

Have a function to Wait until true?

Hi there,

I have a spawn function thats called every time my player dies. It basically turns off all the colliders and renderers on my player, moves it to a 'spawn point' wait a few seconds, and turns the player back on.

Is it possible to have this function to wait until a certain external variable is true, and then carry on?

So, right now it does:

  • Turn off player

  • Move player

  • Wait a few seconds

  • Turn player on

I now want it to do:

  • Turn off player

  • Move player

  • Wait until 'blah' is true

  • Turn player on

If your wondering... that 'Blah' is a weapon list that pops up when you die, and when the player selects the weapon he wants, 'blah' comes true. Thus then continuing to spawn the player.

I know theres all sorts of different methods. But I really want to know if a form of 'yield' is possible?

Thanks

Comment
Add comment
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

4 Replies

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

Answer by awdawdawd · Aug 19, 2012 at 02:13 PM

I figured it out, not as complicated as I thought:

 function Spawn(){
 
    turnOffPlayer();
 
    while(weaponSelected == false){
       yield;
    }
 
    turnOnPlayer();
 }
Comment
Add comment · Show 2 · 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 420BlazeIt · Oct 29, 2015 at 10:56 PM 0
Share

How would you do this in c#?

avatar image Statement 420BlazeIt · Oct 29, 2015 at 11:10 PM 2
Share

See the manual on coroutines.

 IEnumerator Spawn() {
 
     turnOffPlayer();
  
     while(weaponSelected == false){
        yield return null;
     }
  
     turnOnPlayer();
 }


avatar image
38

Answer by aFeesh · Nov 23, 2016 at 07:34 AM

Without the while loop in C# using WaitUntil

 IEnumerator Spawn() {
  
      turnOffPlayer();
 
      yield return new WaitUntil(() => weaponSelected == true);
 
      turnOnPlayer();
  }


Comment
Add comment · Show 4 · 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 Garrom · Mar 03, 2017 at 05:09 PM 0
Share

using lambada ........ GENIAL, why i don't get it myself and googling like idiot ?

avatar image juan-jo · May 12, 2017 at 09:09 AM 0
Share

Why would you make it understandable while you can use some obscure lambda expression?

avatar image idbrii juan-jo · Oct 22, 2020 at 07:31 PM 2
Share

To remove the lambda, you can put it in a normal function:

 bool HasSelection() {
     return weaponSelected;
 }
 IEnumerator Spawn() {
     turnOffPlayer();
     yield return new WaitUntil(HasSelection);
     turnOnPlayer();
 }
avatar image aloften · Jun 16, 2019 at 12:01 AM 0
Share

This is....amazing. Thank you. This is probably the easiest method ive seen so far.

avatar image
1

Answer by Trainzland · Jun 09, 2018 at 02:17 PM

Time_Flys example: Don't forget to use

 StartCoroutine(Spawn());

Otherwise it would not work.

Comment
Add comment · Show 2 · 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 BunnyProductions · Oct 11, 2018 at 01:25 AM -1
Share

Correct me if im wrong, but isnt it

 StartCoroutine("Spawn");

avatar image BunnyProductions BunnyProductions · Oct 13, 2018 at 01:16 AM 0
Share

nvm its the same thing

avatar image
1

Answer by danielmahon · Apr 26, 2019 at 03:53 PM

Using async/await via https://assetstore.unity.com/packages/tools/integration/async-await-support-101056

 async void Spawn() {
     turnOffPlayer();
     await new WaitUntil(() => weaponSelected == true);
     turnOnPlayer();
 }

Note: I love the async/await paradigm (having used it in javascript/typescript) but I only recently started using it C# Unity so I'm not sure of any side-effects but haven't come across any so far. Makes the code soooo much cleaner IMHO.

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 Quatum1000 · Jun 29, 2019 at 12:49 PM 0
Share

await new WaitUntil(() => weaponSelected == true); This version waits per frame until weaponSelected == true. This is already asynchronous!

async void Spawn() .. Could you explain this, please.

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

22 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

Related Questions

Quick question about destroying bullets/objects 1 Answer

[Javascript] Creating waiting points for random movement with NavMesh Script 1 Answer

yield function until a boolean is true? 2 Answers

Yield And Return 2 Answers

Waiting for input using yield and coroutines 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