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 Harryliu · Dec 26, 2015 at 11:11 AM · functionfunction update

Execute IF statement once inside the Update Function?

I didn't get an answer from the last question so now I am asking it in a more clear and straightforward manner.

So here is my script:

 if(hunting == true)
     {
 
         //Displays Hunting Progress
         progressText.text = "% " + progress + " Complete";
 
         canRest = false;
         canHunt = false;
 
         //If progress = 100, Spawn furs.
         if(progress == 100)
         {
             CancelInvoke("consumeFood");
             CancelInvoke("increaseProgress");
 
             canRest = true;
             canHunt = true;
 
             
         
             if(level >= 1)
             {
                 if(!singleExecution)
                 {
                     SpawnFur();
                     singleExecution = true;
                 }
 
             }
         }
     }

This currently in Update Function as it needs to check for "Progress" every frame. But when progress reaches 100, the if statement inside of it will be fired multiple times because it is in Update.

the current set up I have with the singleExecution bool allows it to be fired ONCE, that is all, as the bool will be cleared after the first execution.

However I only want it to be fired ONCE when the condition(Progress == 100) is met EVERY TIME. How do I do that? Any help would be appreciated.

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
1

Answer by allenallenallen · Dec 26, 2015 at 03:45 PM

Try number 2 then.

 public bool canRunProgress = false; // Kind of like the singleExecution

 if(hunting == true)
  {
 
      //Displays Hunting Progress
      progressText.text = "% " + progress + " Complete";
 
      canRest = false;
      canHunt = false;
 
      //If progress = 100, Spawn furs.
      if(progress == 100 && canRunProgress)
      {
          canRunProgress = false;  // Immediately stop this from running more than once.
          CancelInvoke("consumeFood");
          CancelInvoke("increaseProgress");
 
          canRest = true;
          canHunt = true;
 
          
      
          if(level >= 1)
          {
              if(!singleExecution)
              {
                  SpawnFur();
                  singleExecution = true;
              }
 
          }
      }
     else { 
         canRunProgress = true;   // Only when progress isn't 100 anymore can it run again.
     }
  }
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
avatar image
0

Answer by JimNero_009 · Dec 26, 2015 at 03:37 PM

Set up a bool within the if (progress == 100) line. Have something like; if (progress == 100 && progressbool == true){ do stuff and set progressbool to false } Then set up a later condition that sets the progressbool back to true once you're certain it won't fire the code again.

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
avatar image
0

Answer by Sungold · Aug 14, 2016 at 09:37 PM

You'll have to make a separate boolean variable that is initially set to false, and then set it to true in the code, (or the other way around). Then, modify the if statement so that it checks for the initial state of the boolean before executing the code. If needed, you can reset the boolean to false outside the code somewhere, but you may need another one-shot boolean for that too, and it might add up.

Separate question: Is there a monobehaviour extension that can be made to handle these situations? I hate to declare so many boolean variables for all of the pieces of code that only need to run once in Update(). I would love to make a monobehaviour extension for this myself, but I really am not sure how.

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
avatar image
0

Answer by mrpmorris · Aug 15, 2016 at 07:02 AM

You are checking if Progress is equal to 100, what you need to know is if 100 was reached. To know this you only need to know if it was previously not 100.

Put this at the top of your Update method. It makes sure that if progress is ever less than 100 then it resets the boolean so your code knows to re-execute SpawnFur() in a future call to Update() when progress does == 100

 if (progress < 100) {
   singleExecute = false;
 }



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

7 People are following this question.

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

Related Questions

Function Update and error UCE0001 3 Answers

How to call a function only once in Update 1 Answer

How to access variable from another function? 2 Answers

Bullet is shooting every frame? How do I limit it to 1 bullet per second? 3 Answers

GetButton Problem 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