Jump to content
Unity Insider Forum
Sign in to follow this  
KevinRazor

Daily Reward System Fehler

Recommended Posts

Ich habe mit meinem Daily Reward System irgendwie probleme.

Es funktioniert perfekt und auch obwohl der fehler entsteht aber ich möchte ihn schon ganz gerne raushaben nur verstehe ich irgendwie nicht was die fehlerquelle ist :\

In der Console steht folgendes

Zitat

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)
DailyReward.Start () (at Assets/Frontman/Script/GUI/DailyReward.cs:25)

und das Script sieht wie folgt aus

using UnityEngine;
using UnityEngine.UI;
using System;

public class DailyReward : MonoBehaviour
{
    public int secondsToWait = 10;
    public int coinReward = 25;
    public AudioClip soundReward;
    public bool canGetReward = true;
    [Space]
    public Button rewardButton;
    public Text displayText;

    private ulong lastRewarded;
    private ulong difference;
    private ulong milisec;
    private float milisecToWait;
    private float secondsLeft;

    private void Start()
    {
        //Get the <LastRewarded> time.
        string lastSavedTime = PlayerPrefs.GetString("LastRewarded");
        lastRewarded = ulong.Parse(lastSavedTime.ToString());

        //Match the variables.
        if (!CanGetReward())
            canGetReward = false;

        else if (CanGetReward())
            canGetReward = true;
    }

    private void Update()
    {
        //Matching the different variables.
        canGetReward = CanGetReward();

        //Our reward is ready.
        if (canGetReward)
        {
            //Enable the button, and set the display text to <readyText>.
            displayText.text = "Claim Today Reward!";
            rewardButton.interactable = true;
        }

        //We cannot get reward at the moment.
        else if (!canGetReward)
        {
            //Diasble the button, and create an empty string.
            rewardButton.interactable = false;
            string timerText = "";

            //Displaying Hours.
            timerText += ((int)secondsLeft / 3600).ToString("00") + "h ";
            secondsLeft -= ((int)secondsLeft / 3600) * 3600;

            //Displaying Minutes.
            timerText += ((int)secondsLeft / 60).ToString("00") + "m ";

            //Displaying Seconds.
            timerText += (secondsLeft % 60).ToString("00") + "s";

            //Refresh the display text.
            displayText.text = timerText;
        }
    }

    public void GetReward()
    {
        //If we can get the reward...
        if (canGetReward)
        {
            //Set <LastRewarded> to the current time.
            lastRewarded = (ulong)DateTime.Now.Ticks;
            PlayerPrefs.SetString("LastRewarded", lastRewarded.ToString());

            //We just got the reward, so we can't get it again.
            canGetReward = false;

            //Reward can be given to the player here.
            GlobalValue.SavedCoins += coinReward;
            SoundManager.PlaySfx(soundReward);
            Debug.Log("We got our 'Daily' Reward!");
        }
    }

    private bool CanGetReward()
    {
        //Getting the difference between the current time and the <LastRewarded> time.
        difference = ((ulong)DateTime.Now.Ticks - lastRewarded);
        milisec = difference / TimeSpan.TicksPerMillisecond;

        //Since the input wait time is in seconds, we have to multiply by 1000 to get it in miliseconds.
        milisecToWait = secondsToWait * 1000;
        secondsLeft = (float)(milisecToWait - milisec) / 1000f;

        //Check if we can get the reward.
        if (secondsLeft < 0)
            return true;

        else return false;
    }
}

begreife den fehler nicht da ja alles tadelos funktioniert :\

Share this post


Link to post
Share on other sites

Bin mir nicht ganz sicher ob das dein Problem löst aber die ersten beiden Codezeilen un deiner Start-Methode sehen so aus:

 

string lastSavedTime = PlayerPrefs.GetString("LastRewarded");
lastRewarded = ulong.Parse(lastSavedTime.ToString());

in der 2. Zeile versuchst du lastSavedTime in einen string zu convertieren, obwohl diese Variable bereits ein string ist.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×