1
votes

I receive an error:

FormatException: Input string was not in the correct format System.UInt64.Parse (System.String s, NumberStyles style, IFormatProvider provider) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/UInt64.cs:351) System.UInt64.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/UInt64.cs:99) TimerScript.Awake () (at Assets/Scripts/TimerScript.cs:19)

Below is code:

using System;
using UnityEngine;
using UnityEngine.UI;

public class TimerScript : MonoBehaviour {
    public float WaitTime = 7200000.0f;
    public Button OpenButton;
    public ulong lastCheastopen;
    public Text FreeCaseText;

    void Awake()
    {
        lastCheastopen = ulong.Parse(PlayerPrefs.GetString("LastCheast"));
    }

    private void Start()
    {
        OpenButton = GetComponent<Button>();

        if (!IsCheastReddy())
        {
            OpenButton.interactable = false;
        }
    }

    private void Update()
    {
    if (!OpenButton.IsInteractable())
        {
            PlayerPrefs.SetString("LastCheast", lastCheastopen.ToString());
            if (IsCheastReddy())
            {
                OpenButton.interactable = true;
                return;
            }

            ulong diff = ((ulong)DateTime.Now.Ticks - lastCheastopen);
            ulong m = diff / TimeSpan.TicksPerMillisecond;
            float secondsLeft = (float)(WaitTime - m) / 1000.0f;
            int seconds = ((int)secondsLeft % 60);
            int minutes = ((int)secondsLeft / 60) % 60;
            int hours = ((int)secondsLeft / 3600) % 24;

            string TimerString = string.Format("{0:0}H {1:00}Mim {2:00}Sec", hours, minutes, seconds);
            FreeCaseText.text = TimerString;
        }
    }


    public void OkCklickButton()
    {
        lastCheastopen = (ulong)DateTime.Now.Ticks;
        PlayerPrefs.SetString("LastCheast", lastCheastopen.ToString());
        PlayerPrefs.Save();
        OpenButton.interactable = false;
       // lastCheastopen = ulong.Parse(PlayerPrefs.GetString("LastCheast"));
    }

    private bool IsCheastReddy()
    {
        ulong diff = ((ulong)DateTime.Now.Ticks - lastCheastopen);
        ulong m = diff / TimeSpan.TicksPerMillisecond;
        float secondsLeft = (float)(WaitTime - m) / 1000.0f;

        if (secondsLeft < 0)
            return true;

        return false;
    }
}
1
In your awake method. What is the output of the PlayerPrefs.GetSTring("LastCheast"); call? could you save that into a string variable and give us the output pls?Tobias Theel
public ulong lastCheastopen = 636400511496774752; I can not get the value after running the script. and get an errorMax Krivokonev
If you cannot get the value after running the script, then that is the reason for the error: Probably it is empty string or null, therefore it cannot be parsed as a uint.Ben
Just a nit pick here, but shouldn't it be lastCheastOpen for proper camel casing?Andrew

1 Answers

1
votes

try:

lastCheastopen = ulong.Parse(PlayerPrefs.GetString("LastCheast", "0"));