Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ninjixfatality · Jan 25, 2020 at 06:08 PM · unity 2ddatetime

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);
 }











Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges