2D Virtual Pet Game Code - stuck on code

Hi there!

For starters, I’m new to coding so I’ve had no luck in trying to fix this issue myself, and I don’t understand why my code isn’t working like the guy’s code in the video. Right now I’m following a youtube tutorial (Unity3D Tutorial - How to Make a Pet Simulation Game 10: Altering Hunger and Happiness - YouTube) to make a 2D virtual pet game. So far the tutorial has been helpful but when the guy started coding a feature to change the pet’s happiness and hunger levels regarding how much time the player spend away from the game, the game started to have issues.

I followed the exact code of what the person in the video did and my game not functioning like his is. When you set the time of the game to slightly after the real time (e.g. the real time is 15:30pm, so you set the debug code to 16:30, 1 hour ahead), the happiness and hunger level of his game lowers, rather mine increases which isn’t correct.

Any help would be very much appreciated!

Here is my code -

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class ChickenScript : MonoBehaviour
{

    [SerializeField]
    private int _hunger;
    [SerializeField]
    private int _happiness;

    bool serverTime;


    // Start is called before the first frame update
    void Start()
    {
        PlayerPrefs.SetString("then", "14 / 08 / 2020 18:02:12");
        updateStatus();
    }

    void updateStatus()
    {
        if (!PlayerPrefs.HasKey("_hunger"))
        {
            _hunger = 100;
            PlayerPrefs.SetInt("_hunger", _hunger);
        }
        else
        {
            _hunger = PlayerPrefs.GetInt("_hunger");
        }

        if (!PlayerPrefs.HasKey("_happiness"))
        {
            _happiness = 100;
            PlayerPrefs.SetInt("_happiness", _happiness);
        }
        else
        {
            _happiness = PlayerPrefs.GetInt("_happiness");
        }

        if (!PlayerPrefs.HasKey("then"))
            PlayerPrefs.SetString("then", getStringTime());

        TimeSpan ts = getTimeSpan();

        _hunger -= (int)(ts.TotalHours * 2);
        if (_hunger < 0)
            _hunger = 0;
        _happiness -= (int)((100 - _hunger) * (ts.TotalHours / 5));
        if (_happiness < 0)
            _happiness = 0;

        Debug.Log(getTimeSpan().ToString());
        Debug.Log(getTimeSpan().TotalHours);


        if (serverTime)
            updateServer();

        else
            InvokeRepeating("updateDevice", 0f, 30f);

    }

    void updateServer()
    {


    }

    void updateDevice()
    {
        PlayerPrefs.SetString("then", getStringTime());
    }

    
    


    TimeSpan getTimeSpan()
    {
        if (serverTime)
            return new TimeSpan();
        else
            return DateTime.Now - Convert.ToDateTime(PlayerPrefs.GetString("then"));
    }


    string getStringTime()
    {
        DateTime now = DateTime.Now;
        return now.Month + "/" + now.Day + "/" + now.Year + "" + now.Hour + ":" + now.Minute + ":" + now.Second;
    }

    public int hunger
    {
        get { return _hunger; }
        set { _hunger = value; }
    }

    public int happiness
    {
        get { return _happiness; }
        set { _happiness = value; }
    }


}

You have already created a key of _hunger in the player prefs, and i see that when it reaches 0 it get set to 0.
So every time you start you will always have a hunger value of 0 and
This line

_happiness -= (int)((100 - _hunger) * (ts.TotalHours / 5));

is basically
This line

_happiness -= (int)((100 - 0) * (ts.TotalHours / 5));

so it will start to increase up.
try to delete the values in the playerprefs and debug again.