Trouble accessing a set time from input field

I’m working on a count down timer but I’m having trouble accessing the time I set in an input field

I’m calling my timer from update which points to this:

if (StartCDTimer == true)
        {
            // Start Timer
            CDTimer -= Time.deltaTime;
            seconds = (int)(CDTimer % 60);
            minutes = (int)((CDTimer / 60) % 60);
            hours = (int)(CDTimer / 3600);
            hoursCDTField.text = hours.ToString("00");
            minutesCDTField.text = minutes.ToString("00");
            secondsUI.text = seconds.ToString("00");
        }

My input fields are numerical but set as strings.

What do you mean you’re having trouble accessing the time you set?

even though I have my input field to start my timer from it always just starts from zero

Well, your time remaining is whatever is in the variable CDTimer. If you want to start with a different amount of time, you need to put a different number into that variable.

If you have a string but you want to interpret it as a number, you may want to use something like float.TryParse(). If the string is more than just a single number (for instance, if it’s something like “10:52”) then you may need to do something more complicated; either pre-process the string to isolate the information you want, or use a more generalized parsing algorithm.

it’s just coming from two input fields where I just type in up to 2 digits per field, one for hours and the other for minutes:
like this:

public InputField hoursCDTField;
public InputField minutesCDTField;

OK, so get the value from those fields using expressions like “hoursCDTField.text” and then feed it into a function like int.TryParse or float.TryParse.

Not sure I follow, I’ve tried a few different combinations but I guess I’m getting either the order or syntax wrong.

I think it’s already converted as the clock time reads what I set it to in my input fields, it’s just that when I start the timer it’s not using the time already set on the clock, it seems to just start it at all zeros. I should mention that both the hours and seconds are in two different input fields. I think the problem is in here somewhere:

// CDTimer Timer
    public void CDTimerCalcu()
    {
        if (StartCDTimer == true)
        {
          
            // Start Timer
            CDTimer -= Time.deltaTime;
            seconds = (int)(CDTimer % 60);
            minutes = (int)((CDTimer / 60) % 60);
            hours = (int)(CDTimer / 3600);
            hoursUI.text = hours.ToString("00");
            minutesUI.text = minutes.ToString("00");
            secondsUI.text = seconds.ToString("00");
          
        }

    }

I set the clock time like this:

// Set Watch Time To Input Fields For CDT Timer
            hoursUI.text = hoursCDTField.text;
            minutesUI.text = minutesCDTField.text;

Where is the code where you are actually setting CDTimer to what was typed in the input fields? All I see is you set some UI Text components to what is in the input fields, but not where you also adjust CDTimer.

There needs to be some code which parses the input from the input fields, converts that to a float, and sets CDTimer.

Should it not just count down from the time already set on the watch?

The way your code is written “the watch” is the CDTimer variable itself. So yes your statement is true, the problem is you’ve skipped the step of setting the watch.

Hmm I thought it would just count down from the time I had set using the input fields… so what am I missing?

Computers are like evil genies: they give you exactly what you ask for, not what you meant. Computers do not have common sense. They will not “get the gist” of your code or infer your goal. They will follow your instructions exactly as you wrote them, to the letter, no matter how ridiculous the result.

The first code you posted sets all of your fields based on the value of the variable “CDTimer”. It doesn’t matter what was in those fields before; the ONLY thing that matters is the value stored in that variable.

The computer does not know that you are trying to enter a number and count down from that. It’s just doing what you told it to do.

Here’s the disconnect:

In your code you update the watch by subtracting Time.deltaTime from CDTimer. You then set separate integers for hours/minutes/seconds from the value of CDTimer, and you use those separate variables to update some UI Text components to display the time to the user.

When you’re taking input from the user, you’re taking the string values of those input fields and setting those same UI Text components. But the UI Text components are just for displaying the timer to the user, they aren’t the timer itself, and the next time you start the watch again it is going to subtract Time.deltaTime from CDTimer, the same ints again, and the UI Text components, all based on the value of CDTimer. Everything in your code runs off CDTimer, so if you want to change the value of the timer from user input, the only thing that actually matters as far as actually setting the timer is setting the CDTimer variable itself.

Think of it like this. You’ve got a computer, and you have a monitor. CDTimer is the computer, and the UI Text fields are the monitor. You now want to shut down and restart the computer, so you go to the monitor and press the power button to shut it off, then press the power button to turn it on. Surprise! The computer didn’t restart, and it is just displaying the same thing it did before you tried. Why?

You’re effectively doing the same thing here. When you’re taking the user input, you’re only using the input to update the user friendly display of the timer, but not the timer itself. The timer itself again is the CDTimer variable in your code. Take the input from the input fields, and use that to update CDTimer, since everything else in your watch is driven off the value of CDTimer.

1 Like

Ahh, I get it so the UI text is just simple being updated by my:

// Set Watch Time To Input Fields For CDT Timer
                hoursUI.text = hoursCDTField.text;
                minutesUI.text = minutesCDTField.text;

But what you are saying is the real time value is being stored in:

float CDTimer;

So how for example would I actually change the value of the float CDTimer with the value from my input fields??
I do have those set in the inspector to only accept numerical values and only allow 2 digits to be typed into each field, not that I believe they are related in any way to my problem.

I believe also that I’m storing each value separately according to what I have here:

 float seconds;
    float minutes;
    float hours;

I tried this as an experiment but it still just started my timer at 00:00:00

float.TryParse(hoursCDTField.text, out hours);

I keep searching for how to do this but I keep getting either how to convert an int into a string or other unrelated search results.

Heck I’ve even tried this!!

float h = hours;
float.TryParse(hoursCDTField.text, out h);
float m = minutes;
float.TryParse(minutesCDTField.text, out m);

When you did this, did you assign h and m to the text objects? Did you debug h and m to see what values they were?

I tried:
Debug.Log(CDTimer);
At the end of my code and it still said CDTimer was 0?

I tried this:

float.TryParse(hoursCDTField.text, out CDTimer);
Debug.Log(CDTimer);

And in the console it showed that CDTimer was what I had typed into my input field but when I started th timer is still started at all 0’s