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 Marks981 · Apr 04, 2015 at 02:57 PM · timetimerifdeltatime

If "equals" doesnt work.

In this script you can see how time goes up from 0 (using Time.deltaTime). In my case,i want that unity prints massage for me when time is exactly 35 (FirstPass) Function works if time is greater than or less than Firstpass,or second or third. But when i want that time is exactly equals to FirstPass,which is 35...the massage is not showing... By the way it works only when i disable time counting up and enter the value manually.... Why? Please.

 #pragma strict
 
  var time : float = 0.0f;
  var FirstPass : float = 35.0f;
  var SecondPass : float = 45.0f;
  var ThirdPass : float = 55.0f;
  
  var SpeedOfTime : float;
  
 
 function Update ()
 {
    
   //if(Input.GetKeyDown(KeyCode.Space))
     
    TimeStarts();
    time += Time.deltaTime * SpeedOfTime;
 }
 
 
 function TimeStarts ()
 {
 
     if(time == FirstPass)
     {
         
         print("FirstPass");
     }
    
     if(time > SecondPass)
     {
         
         print("SecondPass");
     }
 
     else if (time == ThirdPass)
     {
         
         print("ThirdPass");
     }
 }
 
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

3 Replies

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

Answer by tanoshimi · Apr 04, 2015 at 03:00 PM

Because there's only a very small chance of that line of code being executed in a frame after the game has been running for exactly 35 seconds...

Comment
Add comment · Show 5 · 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 Marks981 · Apr 04, 2015 at 06:07 PM 0
Share

Thumbs up for everyone! Thanks for help guys. Is there any other way to approach this? Well if i do "greater than" 35,how can i make that massage print Only Once,not continuously?

avatar image _joe_ · Apr 04, 2015 at 06:16 PM 1
Share

use a boolean as a flag

avatar image hahahaha · Apr 04, 2015 at 06:20 PM 1
Share

While I'm sure there's probably a better way to do this overall, I would probably use bools :)

 public bool FirstPassDone;
 public bool SecondPassDone;
 public bool ThirdPassDone;
 
 
  function TimeStarts ()
  {
  
      if(time > FirstPass)
      {
         if (FirstPassDone == false)
         FirstPassDone = true;
         print("FirstPass");
         }
      }
     
      if(time > SecondPass)
      {
          
        if (SecondPassDone == false)
         SecondPassDone = true;
             print("SecondPass");
        }
      }
  
      else if (time > ThirdPass)
      {
          
         if (ThirdPassDone == false)
         ThirdPassDone = true;
             print("ThirdPass");
         }
      }
  }

Sam

avatar image ozturkcompany · Apr 04, 2015 at 06:26 PM 1
Share

Hello again $$anonymous$$ark, if you look at the answer that i've written its possible to do it by using equals, no need for bigger or smaller comparasion. Do you need me to write the code?

avatar image Marks981 · Apr 04, 2015 at 07:54 PM 0
Share

Hey,thank you guys again,for everything. I figured out,that the best way would be using booleans,it works very well,thanks Sam for that. And ozturkcompany,i really have no idea how to use $$anonymous$$athf. functions :D But,thank you anyway.

avatar image
2

Answer by _joe_ · Apr 04, 2015 at 03:01 PM

You're testing in an Update, The condition will basically never work, because it's changing so fast odds are it's going to skip the exact value.

In the Update it's always best to test for =, that way you can be sure that the value passed, or better, find a different approach for your tests.

Cheers (y)

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 Owen-Reynolds · Apr 04, 2015 at 04:55 PM 1
Share

I think a greater-than sign fell off (the = is really a "greaterThan=". HT$$anonymous$$L stripping does that. I think marking as code will work, test: >=. And an html-style test: >= .

avatar image
2

Answer by ozturkcompany · Apr 04, 2015 at 03:18 PM

Most of the time it won't work because the value of time won't be the exact value that you defined in your variables. It is mostly likely that you could get a result like this; time = 35.00000123 and your FirstPass = 35.00000000 which these two values don't match. What you can do is comparing these two values with the following, either you comparing using Mathf.Approximate or just Round your time value using Mathf.Round and it will be all fine then.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Total Time Played since Installation 2 Answers

countdown timer acivation 1 Answer

Accumulating deltaTime oddity 1 Answer

Accurate timing while using timeScale. 1 Answer

I cant get this timer to start when this bool turns true? 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