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 JessUnity · Jul 23, 2018 at 02:27 PM · booleantime.deltatime

Check how long a bool has been true

Hi,

I saw other threads on this but still couldn't get it to work. I want something to happen after my bool has been true for 5 seconds. I found this piece of code but it doesn't work:

  float boolTime = 0;
  private bool _switch = false;
  public bool switch {
      get {
          return switch;
          }
      set {
          _switch = value;
          if(_switch == true) {
              boolTime = Time.time;
              }
         }
        }
  
  
  void Update() {
  
        if(_switch && (Time.time - boolTime) > 5f) {
           print("Success");
        }
  }


Any tips on how to make this work? Thanks a lot!

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
1

Answer by Stratosome · Jul 23, 2018 at 02:35 PM

Hi there!

I get what your code there is supposed to be doing. Personally, I'd just make little timers with coroutines. That way you wouldn't have to clutter your Update with little bool timers or whatever and they could just run on their own. BUT, I think the issue with your code is that you can't have the one thing called "switch". That's a reserved word, so trying to use it for your variable like thing there is causing issues. If you name it to anything else, it should work. Works for me! Although if that's not the issue, let me know and I'll give it another go hah.

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
1

Answer by JVene · Jul 23, 2018 at 06:23 PM

When you're writing in an object oriented language like C#, part of the technique is to recognize when an object (a class) is hinting its existence, but isn't yet there. This is one of those occasions, and an opportunity to expand into thinking in terms of objects to handle jobs like this.

The hint comes from the observation that you have a property named switch (which is a keyword you can't use, Google C# switch for examples), and the related boolTime that manages it.

Combined, these two hint at the object that should exist. In C#, structs can be used for small, self contained objects just like classes, so you could use either in this situation. Create the type with something like:

 struct TimedBool
 {
  private bool  state;
  private float  timeLastSet;
 
  public bool IsTrueDelayed( float d )
     {
      if ( ( Time.time - timeLastSet ) > d ) return state;
      return false;
     } 

  public bool Set( bool s )
    {
     state = s;
     timeLastSet = Time.time;
    }
 }

This is an illustrative psuedo code (that would probably work), but you can flesh this out as you prefer. You could, for example, make state a property as you did in your post. You could consider conversion operators for convenience. The point is you can only set 'state' by going through code which stamps the time. Later, you check for state with something like:

 private TimedBool   theTimedSwitch = new TimedBool();
 
 void SomeTriggerFunction()
 {
  theTimedSwitch.Set( true );
 }
 
 void Update()
 {
  if ( theTimedSwitch.IsTrueDelayed( 5f ) ) { ...stuff to do when true after 5 seconds....}
 }


Notice this did not bother to check about setting the delay only if setting the bool to true. In your code, you've elected not to set the time when the bool is set to false, but this isn't necessary. Logically, if false is the state, the delay doesn't even matter at all. So, just set the time stamp on all changes to state, it doesn't impact the outcome.

Now you can use a timed bool in any circumstance, have many of them, and not clutter code with multiple related, synchronized tests. The class (or struct) TimedBool synchronizes these two ideas into a single concept. That's what objects are for.

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

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

Related Questions

Delay Player From Using Action Button For A Period Of Time 0 Answers

Log a quickly changing value without boolean flags 0 Answers

How can i keep making the SendMessage repeat as long as the boolean is true 1 Answer

Public variable instance of custom class is never null 2 Answers

Raycast Hit not toggling variable if stops hitting 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