- Home /
DateTime.Now only working using Date After and not Current Date
I would like to know why when I start my game the character has a Happiness and Hunger at 0. I feel like the code line PlayerPrefs.SetString("then", "01/25/2020 12:16:12");
could be the problem.
When I change the date to PlayerPrefs.SetString("then", "01/26/2020 12:16:12");
a day ahead it shows the value of Happiness and Hunger but not on the day of playing and starting the game.
I would like to start the game fresh at 100 for both happiness and Hunger. I would also want to know how I can set up System.DateTime.now
a little better .
I'm following this tutorial in case anyone is lost with the way the code is set up here:
Accessing Device Time: https://tinylink.net/lX5hY
Altering Hunger and Happiness: https://tinylink.net/nV8L2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Robot : MonoBehaviour {
[SerializeField]
private int _hunger;
[SerializeField]
private int _happiness;
[SerializeField]
private string _name;
private bool _serverTime;
private int _clickCount;
void Start() {
PlayerPrefs.SetString("then", "01/24/2020 12:16:12");
UpdateStatus();
if (!PlayerPrefs.HasKey("name"))
PlayerPrefs.SetString("name", "Robot");
_name = PlayerPrefs.GetString("name");
}
void Update()
{
if (Input.GetMouseButtonUp(0))
{
Vector2 v = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(v), Vector2.zero);
if (hit)
{
if(hit.transform.gameObject.tag == "Pet")
{
_clickCount++;
if (_clickCount >= 3){
_clickCount = 0;
updateHappiness(5);
}
}
}
}
}
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;
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; }
}
public string name
{
get { return _name; }
set { _name = value; }
}
public void updateHappiness(int i)
{
happiness += i;
if (happiness > 100)
happiness = 100;
}
public void saveRobot()
{
if (!_serverTime)
updateDevice();
PlayerPrefs.SetInt("_hunger", _hunger);
PlayerPrefs.SetInt("_happiness", _happiness);
}
Answer by sacredgeometry · Jan 25, 2020 at 06:48 PM
Note: Only non-abstract, non-generic custom classes can be serialized.
https://docs.unity3d.com/ScriptReference/Serializable.html
Perhaps use composition. i.e. move them into their own class and then add a filed of that type to your monobehavior.
Other than that I dont think properties are serialisable in unity so property backed fields may also not be.
Your answer

Follow this Question
Related Questions
Spring in Hingejoint2D 1 Answer
MoveTowards not working 1 Answer
Particle System 2D dont works in Game 0 Answers
Why Does it not Reset levels? 0 Answers
Changing a set value over time and if player presses a button to change rest of the value at once 0 Answers