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 23, 2015 at 09:54 AM · function update

Calling a function in Update once every time when the condition is met?

I have a game in which one random item is spawned when a timer reaches zero. This is currently in the Update function as it needs to check the timer every frame. But when timer reaches zero, it spawns multiple items because it is in the Update function. I have tried:

 if(!singleExecution)
 {
         
            if(this condition is met)
            {
                      execute this action;
            }
 
            singleExecution = true;

 }


this allow the function to be executed once in the Update function when the condition is met the first time. However it does not get executed once every time the condition is met, which exactly is what I need. So Update function doesn't work, what does?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by allenallenallen · Dec 23, 2015 at 09:57 AM

You have an error there. What if the condition isn't met but the timer still reached zero? Then the inner if statement will be discarded BUT the boolean singleExecution will still set to be true.

This is how it should be:

 if (!singleExecution)
  {
      if(this condition is met)
      {
         singleExecution = true;   // This boolean should be inside the if statement
         execute this action;
      }
  }
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 Harryliu · Dec 23, 2015 at 05:40 PM 0
Share

the condition is the timer. when the timer reaches zero, the condition is met.

avatar image
2

Answer by wibble82 · Dec 23, 2015 at 10:25 AM

@allenallenallen pointed out the first issue with your logic, however I think you are also saying that you want your code to:

  1. 'execute this action' once when the condition is first met

  2. wait until it is no longer met

  3. 'execute this action' when it is met again

  4. goto 2

To do this, you would take a similar fix to the one proposed by @allenallenallen :

 //check if the condition is currently met
 if(this condition is met)
 {
     //it is met, so check if we have executed yet
     if(!singleExecution)
     {
         //we hadn't executed yet, so set the single execution bool and execute
         singleExecution = true;
         execute this action;
     }
 }
 else
 {
     //it isn't met, so clear out 'singleExecution' bool
     singleExecution = false;
 }

-Chris

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 Harryliu · Dec 23, 2015 at 11:54 PM 0
Share

This doesn't really work for me. The action gets executed once when I run the game, and when the condition is met the second time, the action does not get executed because singleExecution is set to true after the first execution.

avatar image wibble82 · Dec 24, 2015 at 02:01 PM 0
Share

Oops - my bad - line 15 should be setting it to false

avatar image wibble82 · Dec 24, 2015 at 02:02 PM 0
Share

Fixed it :)

avatar image Harryliu · Dec 24, 2015 at 11:47 PM 0
Share

Well... it kind of hard to explain so I will post the whole thing.

 //If progress = 100, Spawn items.
 if(progress == 100)
 {
     CancelInvoke("consumeFood");
     CancelInvoke("increaseProgress");
     canRest = true;
     canHunt = true;

     if(level >= 1)
     {
         if(!singleExecution)
         {
             SpawnFur();
             singleExecution = true;
         }

     }

     else
     {
         singleExecution = false;
     }
 }

the "Level >= 1" condition is always met, as the player's lowest level is 1. So the ElSE statement that clear out the singleExecution will never be executed. How can I make this script, so that when the progress reaches 100, spawns 1 item, while checks the progress every frame?

Sorry if I wasn't clear with my question.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Quaternion , are Euler angles updated in the same update function? 2 Answers

Endless runner, road segments spawn speed 0 Answers

my object instantiates to much 1 Answer

BCE0023 Error 1 Answer

Unity increasing more than i want! 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