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 Rhylvin2015 · Aug 17, 2015 at 04:58 AM · c#loopframerateifcheck

how to check only once when a boolean is always true on update. it keeps looping

hey I've got a C# script that I can't seem to make a simple logic. I want it to check only once because this variable is always true making it loop again and again. I want it to check only once so that it will work just once even though the variable is always true. so basically what i need is a simple logic script.

here's a simple script on my dilema to give you an idea on what my problem is

 private bool hold;
 private bool jumpOnlyOnce;
 
 Update()
 {
    if(hold == true)//when I Hold, it will return to true every frame
    {
        if(jumpOnlyOnce == true)
        {
             jumpOnlyOnce = false;//when its true it will jump
        }
        else
            jumpOnlyOnce = true;
    }
 }
 // i actually got this logic from a Pause Button, the Difference 
 //is. a Pause Button only gets push once so its not going through 
 //every frame that's why this wont work since this script is hold 
 //which is true until its let go. I want it to work only once  
 //even though hold is always true.




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 Evansbsr · Aug 17, 2015 at 05:53 AM 0
Share

The code you've written sets the jumpOnlyOnce variable to false but resets it to true during the next Update() loop. This is what creates the looping problem. Try removing the line that resets the jumpOnlyOnce variable to true. Set that variable to true when the hold variable is false

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Eno-Khaon · Aug 17, 2015 at 05:18 AM

So, if I understand right, you're asking if there's a way to automatically assume that the boolean value is "true" after the first time so it doesn't have to keep checking?

Sorry to tell you this, but that's COMPLETELY unnecessary. Also, there aren't really good ways of doing that.

A check of whether a boolean is true or false is one of the fastest tests you can perform in programming. In general, when you're worried about squeezing out minute fragments of performance, you convert things INTO booleans for a faster comparison. For example, you might compare floats and store the result as a bool to call up more quickly.

But for a once-per-frame test, you'll need a few billion instances of that script before you would even possibly notice any performance impact from testing whether it's true or false.

However, what you would more likely notice first is that the Update() functions themselves, let alone billions of instantiated objects and their object and transform data, would be taking a far greater toll on your performance than a single boolean would.

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 etopsirhc · Aug 17, 2015 at 05:26 AM 1
Share

i believe he wants it to not check every frame so that the jumps wont be flipping back and forth every frame. think super mario. hold jump for full height, but tap it for a short jump. if he lets it loop as is he would only ever get a short jump.

avatar image
0

Answer by etopsirhc · Aug 17, 2015 at 05:20 AM

what you want to do is to check for a change in the variable

 if (hold == true && lastHold == false){
     // do your jump logic
     lastHold = hold;
 }else if(lastHold == true && hold == false){
     lastHold = hold;
 }

this will only trigger when hold and lastHold are not the same, then before each part closes lastHold gets the value of hold to keep them the same any time you dont want them to run.

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Nested Array? Populating a menu using a loop. 0 Answers

GameObject tag to if condition 1 Answer

Using ForEach Loop to change variables on prefabs 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