• 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 Cornelius12 · Nov 16, 2018 at 01:45 PM · values

I need help with simple timescript

public class Timescript : MonoBehaviour {

 public int maxTime = 24;
 public int Maxyear = 365;

 public float theTime;
 public float day;

 // Use this for initialization
 void Start()
 {
     theTime = 0;
 }

 // Update is called once per frame
 void Update()
 {
     if (theTime < maxTime)
     {
         theTime += 3 * Time.deltaTime;

     }
     if (theTime == maxTime)
     {
         day =+ 1;
         theTime = 0;
     }
     if (day == Maxyear)
     {
         Endgame();
     }

 }

 void Endgame()
 {

 }
 

}

Problem

When theTime = 24, i want it to add a day. But when it reaches the number. It stops. Im not sure what Im doing wrong...

Thank U for taking your time and helping me.

Note: Im new on Unity so try to explain stuff...

thanks

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
1
Best Answer

Answer by Tomer-Barkan · Nov 16, 2018 at 01:55 PM

You're checking for equality in floats:

  if (theTime == maxTime)

It's very unlikely that the frame time (deltaTime) will be a whole number, so you probably end up with theTime being 23.98456 and next frame it jumps to 24.0012348 (or some other number of the sort).

It's never actually equal to 24.00

The solution though is easy, simply do a greater or equal comparison instead of equality:

  if (theTime >= maxTime)

Same goes for max year.

Comment
Add comment · Show 4 · 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 Cornelius12 · Nov 16, 2018 at 02:04 PM 0
Share

@Tomer-Barkan

Thanks for your help. That works. But the maxnumber for day is 1 It seems like. Do you know whats going on with that?

Edit: theTime restart as it should but the Day stays as 1. IfUknowwaddamean.

avatar image Tomer-Barkan Cornelius12 · Nov 16, 2018 at 04:19 PM 0
Share

Oh Duh,

day =+1 should be day += 1

avatar image Cornelius12 Tomer-Barkan · Nov 16, 2018 at 05:02 PM 0
Share

@Tomer-Barkan Yes, now it works perfect. Thank you. Really appriciate it.

Show more comments

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

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

Related Questions

How to smootlhy change values in amout of time? 3 Answers

Synchronizing multiple values 1 Answer

How do I create a Key and Values Dictionary array in C# 1 Answer

Adding the values of two functions together to display the total 1 Answer

How to have Database of Dictionary elements with online images??? 0 Answers

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