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
0
Question by Giannis7312777 · Jul 24, 2013 at 12:34 PM · timeyield

yield WaitForSeconds

Hello eveyone, is there any way to do if statement with yield WaitForSeconds? Like,

 if(yield WaitForSeconds(5)){
   //do stuff here
 }

I tried but it doesn't work. Any suggestions?

Thanks :)

Comment
Add comment · Show 4
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 Dave-Carlile · Jul 24, 2013 at 12:36 PM 0
Share

What are you trying to accomplish? The yield will wait, then begin executing the code after the yield is done waiting. What are you trying to accomplish with the if?

avatar image Dimling · Jul 24, 2013 at 12:38 PM 0
Share

Don't think yield can be used like that no. WaitForSeconds can only be used with a yield statement in coroutines.

Have a look at this great earlier answer for a nice description of how to use yield waitForSeconds :)

http://answers.unity3d.com/questions/350721/c-yield-waitforseconds.html

avatar image Giannis7312777 · Jul 24, 2013 at 12:43 PM 0
Share

$$anonymous$$y code is this:

 function OnTriggerEnter (col : Collider){
     if(col.gameObject.tag=="Player"){
         var mesh : $$anonymous$$esh = GetComponent($$anonymous$$eshFilter).mesh;
         mesh.Clear();
         collider.enabled = false;
         audio.Play();
         yield WaitForSeconds(1);
         Invetory.inventoryArray[0]++;
         Destroy(gameObject);
     }
 }


This is when the player hits this cude, it adds 1 health potion to his invetory after 1 second

So, I tried to hack my game with Cheat Engine [add more health potions] and it works. I want to say that if a potion added in less than one second then remove it. Any suggestions?

avatar image BimSekai · Jul 24, 2013 at 12:52 PM 0
Share

I think Dimling is right, you have to use it in a coroutine. Ex:

 function OnTriggerEnter (col : Collider){
     if(col.gameObject.tag=="Player"){
        var mesh : $$anonymous$$esh = GetComponent($$anonymous$$eshFilter).mesh;
        mesh.Clear();
        collider.enabled = false;
        audio.Play();
        StartCoroutine(WaitAndDo(1));
     }
 }
 
 function WaitAndDo (waitTime : float) {
         // suspend execution for waitTime seconds
         yield WaitForSeconds (waitTime);
         Invetory.inventoryArray[0]++;
         Destroy(gameObject);
     }

Does this suits you ?

1 Reply

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

Answer by Jamora · Jul 24, 2013 at 12:57 PM

You should just remember the amount of potions in the inventory before the wait and check if it's the same afterwards. Now, if there are other, allowed, ways of getting potions you will have to update my numberOfPotions accordingly, because this script doesn't discriminate.

Basic idea:

     function OnTriggerEnter (col : Collider){
                 
         if(col.gameObject.tag=="Player"){
             int numberOfPotions = Invetory.inventoryArray[0];
             yield WaitForSeconds(1f);
             if(Invetory.inventoryArray[0] > numberOfPotions){
                 //Player has cheated... possibly. 
                 Invetory.inventoryArray[0] = numberOfPotions;
             }else{
                 Invetory.inventoryArray[0]++;
                 Destroy(gameObject);
             }
             
         }
     }
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 rhys_vdw · Jul 24, 2013 at 01:22 PM 0
Share

int and 1f are both C# only (should be var and 1). Otherwise this solution is correct.

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

20 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

Related Questions

How does Invoke cope with mapping on frames 1 Answer

Increase variable for a few seconds 2 Answers

Temporarily disable blayer movement? 1 Answer

What is the most CPU efficient and smooth way to create timed events? 2 Answers

Waitforseconds vs Time 2 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