• 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 /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Oct 21, 2016 at 12:06 AM · timebooleanif-statementsbooltimers

Get Time A Bool Has Been True

I have a public bool that I would like to track how long it has been set to "True" Is there a way I can determine from an If statement how long the bool has been active for?

Something like: if (somebool activeTime == ?) Then do something

Comment
Add comment · Show 2
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 TreyH · Oct 21, 2016 at 02:03 AM 0
Share

use a get / set with a float for time.

avatar image KnightRiderGuy · Oct 21, 2016 at 02:55 AM 0
Share

$$anonymous$$aybe I worded my question wrong, I was looking for some way to track how long the bool had been true for in an if statement I'm using in a coroutine.

The bool changes from false to true based on input from an external input, a motion sensor.

So I was wanting to do something like:

if the bool has been false for something like 20 or 30 seconds then: do something

not sure if that makes more sense?

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by srylain · Oct 21, 2016 at 12:49 AM

When you set the bool to true, you can also store the Time.time value at the same time. Time.time is the time in seconds since the game started, so when you need to check how long that bool had been true just compare it to the current Time.time value.

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 Itzhak_P · Oct 21, 2016 at 05:28 AM

Hi This is a quick and dirty way ,

     private bool _Enabled;
     private float TimeFromBoolStart = 0;
     public bool yourBool
     {
         get
         {
             return _Enabled;
         }
         set
         {
             _Enabled = value;
             if (_Enabled)
             {
                 TimeFromBoolStart = Time.realtimeSinceStartup;
             }
         }
     }
 
     public float GetTimeSinceBool()
     {
         if (yourBool)
         {
             return Time.realtimeSinceStartup - TimeFromBoolStart;
         }
         else
         {
             return 0;
         }
     }

Good Luck Sfc

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 Itzhak_P · Oct 21, 2016 at 07:27 AM

For some Reason my older post was Discarded

this is quick and dirty script

     private bool _Enabled;
     private float TimeFromBoolStart = 0;
     public bool yourBool
     {
         get
         {
             return _Enabled;
         }
         set
         {
             _Enabled = value;
             if (_Enabled)
             {
                 TimeFromBoolStart = Time.realtimeSinceStartup;
             }
         }
     }
 
     public float GetTimeSinceBool()
     {
         if (yourBool)
         {
             return Time.realtimeSinceStartup - TimeFromBoolStart;
         }
         else
         {
             return 0;
         }
     }


Good luck SFC

Comment
Add comment · Show 2 · 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 KnightRiderGuy · Oct 21, 2016 at 01:48 PM 0
Share

Thanks @Itzhak_P, Now would this reset the bool time for when the bool became "false" again?

avatar image Itzhak_P · Oct 21, 2016 at 08:30 PM 0
Share

Hi Yes it will

avatar image
0

Answer by Naphier · Oct 21, 2016 at 02:12 AM

This is a great use-case for accessors! These can allow you to hook in other behaviour to when the variable has changed. In this case it would be something like this:

 class MyClass 
 {
     private bool _dontSetMeDirectly;
     private float setMeDirectlyChangeTime;
     public bool setMeDirectly
     {
         get { return _dontSetMeDirectly;}
         set 
         {
              if (value != _dontSetMeDirectly)
                  setMeDirectlyChangeTime = Time.time;

              _dontSetMeDirectly = value;
         }
     }

     void UsageExample()
     {
         setMeDirectly = true;
         Debug.Log("setMeDirectlyChangeTime: " + setMeDirectlyChangeTime);
      }
 }

Simple don't ever set the value of _dontSetMeDirectly outside of the accessors (get/set). And don't change the setMeDirectlyChangeTime outside of the setter. Play around with accessors (getter/setters) and learn a bit about them, they are awesome.

Comment
Add comment · Show 3 · 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 KnightRiderGuy · Oct 21, 2016 at 04:46 PM 0
Share

Thanks @Naphier, This method looks interesting too. How would I access that in something like this where I want to execute the Debug.Log("Looks Like They have left the area") and the rest when the bool has been false for about 20 or 30 seconds? I'm calling my bool: Human$$anonymous$$otionDetected

 if ((answerWasGiven == false) && (Human$$anonymous$$otionDetected == false)){
                     Debug.Log ("Looks like they Have Left the Area.... Lets go back to LOITER");
                     inIntimidateConvo = false;
                     yield return new WaitForSeconds (1.0f);    //Wait Time
                     Loiter ();
 
                 }
avatar image Naphier KnightRiderGuy · Oct 21, 2016 at 07:07 PM 0
Share

I don't know what Loiter does so I can't get too in depth. Hopefully it resets some states.

So I'd do this like so:

 float timeOut = 20f;
 bool isCoroutineRunning = false;
 void HandleGoToLoiter()
 {
     if (!answerWasGiven && 
         !Human$$anonymous$$otionDetected && 
         (Time.time - Human$$anonymous$$otionDetectedTime) > timeOut)
     {
         if (!isCoroutineRunning)
         {
             StartCoroutine(GoToLoiter());
         }
     }
 }

 IEnumerator GoToLoiter()
 {
     if (isCoroutineRunning)
         yield break;

     Debug.Log("Looks like they have left the area, go to loiter in 1 sec");
     yield return new WaitForSeconds(1);
     Loiter();
     isCoroutineRunning = false;
 }

avatar image KnightRiderGuy Naphier · Oct 26, 2016 at 01:07 PM 0
Share

Thanks @Naphier, Sorry for the late reply, I was busy as heck working on the voice response part of the project. Um, I'm not sure about the line:

 (Time.time - Human$$anonymous$$otionDetectedTime) > timeOut)

$$anonymous$$y Bool For human motion Being detected is: Human$$anonymous$$otionDetected = false;

Should there be a "." between "Human$$anonymous$$otiondetected" and "Time"

Loiter mode is kind of a stand by mode so basically he goes back into loiter if no motion was detected and enough vocal responses were either positive or neutral, I haven't set up a score count for that part yet so I can track how many there have been.

But yeah Loiter does reset some bools back to false so when motion is detected again he goes through the "Query" mode to determine if there is any sort of threat.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

61 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

Related Questions

how do i say this 1 Answer

Why doesn't this if-statement work? 1 Answer

Find bool in multiple scenes? 0 Answers

Call a function When a bool changes value? 2 Answers

Three button combination to open next scene 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges