Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GameGuy · Apr 04, 2012 at 02:36 PM · time.time

How can I get a fixed time value with using Time.time?

How can I get a fixed time value out of Time.time and compare it with a defined float?

I really don´t know if I understand Time.time right, so if I something here is wrong pleas correct me...

  1. Time.time is read only, and shows me the Time passed since my Scene started if I have it in start function.

  2. If I want to use a Trigger to display the current Time at the triggering I do :

function OnTriggerEnter()

{

timeOnHit = Time.time;

Debug.Log("TheTimeOnTriggerHit" was: + timeOnHit);

}

3.But if I understand right my "timeOnHit" variable is not saved with the value it has at the Debug.Log because if I try to compare it to a float nothing happens:

function OnTriggerStay()

{

if(timeOnHit - Time.time >= 1.0)

{

Debug.Log("Time passed");

} }


Also this is not working in a new project on a simple cube attached:

var startTime;

var timeOnHit;

//

function Start()

{

startTime = Time.time;

}

// function OnMouseEnter()

{

 timeOnHit = Time.time;
     Debug.Log("TheTimeOnTriggerHit was: "+ timeOnHit);

}

//

function OnMouseDown()

{

Debug.Log("Mouse is Down");

if(startTime - timeOnHit >= 1.0)

{

Debug.Log("Time passed, IF statement executed");

}

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Datael · Apr 04, 2012 at 03:04 PM

Try:

 if(Time.time - timeOnHit >= 1.0)


The code you have currently will always render a negative number since Time.time will be greater than timeOnHit.

It's always worth Debug.Log()ing the results used in comparisons when things don't go quite as you'd expect them to; noticing that variables are the wrong way round is often an easily missed mistake and you would have soon noticed your error this time around.

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 GameGuy · Apr 04, 2012 at 03:36 PM 0
Share

It has no effect, the if statement isn´t executed...I thought that maybe something else is messed up in the Project or in Unity so I tested it in an clean new Project with the same result!! Here is the script just put it on a cube or a plane:

var startTime; var timeOnHit; // function Start() { startTime = Time.time; } // function On$$anonymous$$ouseEnter() { timeOnHit = Time.time; Debug.Log("TheTimeOnTriggerHit was: "+ timeOnHit); } // function On$$anonymous$$ouseDown() { Debug.Log("$$anonymous$$ouse is Down"); if(startTime - timeOnHit >= 1.0) { Debug.Log("Time passed, IF statement executed"); } }

avatar image Datael · Apr 04, 2012 at 03:57 PM 0
Share

Of course it won't get executed; Time.time will always be greater than the timeOnHit since it is always recorded (unless you click it in the exact same frame as you put your mouse over it) afterwards, making Time.time a larger number than timeOnHit. Therefore, the result will always be negative (except in the exact same frame case I mentioned earlier where it will be 0), and a negative number is never greater than 1, rendering the code inside the if statement unreachable. To get any comparison you're going to have to flip the variables around like I mentioned in my initial answer...

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

increment in a value with time.deltatime/time.fixeddeltatime 1 Answer

How to measure time independently on frame rate? 2 Answers

slow look at question 1 Answer

Hypothetical: What happens if Time.time overflows? 4 Answers

noobNeedsHelp with writing script for movement 0 Answers


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