I Need a little help With player Prefs

OK I have this script:

using UnityEngine;
using System.Collections;

public class UserInformation : MonoBehaviour {

    public static string LastThingSaid01{
        get; set;
    }

    public static string LastThingSaid02{
        get; set;
    }

    public static System.DateTime startDate;
    public static System.DateTime today;



    /*public static void SetStartDate()
    {
        startDate = System.DateTime.Now ; //save the start date ->
        PlayerPrefs.SetString("DateInitialized", startDate.ToString()) ;
        print ("Setting Start Date Now");
    }*/

    public static string GetDaysPassed()
    {
        today = System.DateTime.Now ;

        //days between today and start date -->
        System.TimeSpan elapsed = today.Subtract(startDate) ;

        double days = elapsed.TotalDays ;

        return days.ToString("0") ;
    }
}

What I’m trying to do or want to do is use the GetDaysPassed information into an if statement so I can use it something like:

if(daysPassed == 1){
do something
}

In my main script just fro testing I have and simple onGUI display just for testing so that I can see the information like this:

void OnGUI()
    {
        GUILayout.Label("Days Passed : " + UserInformation.GetDaysPassed()) ;
    }

But I’m having difficulty figuring out how to get that information in the OnGUI display into and if stement like I mentioned… something like:

if (daysPassed == 1){
do something
}

Any help would be appreciated.

Hi zentaiguy,

I wouldn’t convert the days passed to a string, except for when it’s needed by the GUI, and then just save the days passed number to a daysPassed variable, as so:

daysPassed = UserInformation.GetDaysPassed();

//and

GUILayout.Label("Days Passed : " + daysPassed.ToString("0");

Thanks @TBranflakes

but I’m not really sure what you mean here… I don’t think it’s what I need… ??
What i’m trying to do is pass the information in UserInformation.GetDaysPassed()

Which is on my UserInformation script like this:

public static string GetDaysPassed()
    {
        today = System.DateTime.Now ;

        //days between today and start date -->
        System.TimeSpan elapsed = today.Subtract(startDate) ;

        double days = elapsed.TotalDays ;

        Debug.Log ("It Was this Manys Days Since you Asked Me That Last " + days.ToString("0"));


        return days.ToString("0") ;

    }

But then in my main script I’m dong this:
In the If statement I want to check the days past to basically see if the current day is one day ahead of my saved SetStartDate… hopefully that made sense?

case "AreYouAspaceship":
                LoadInformation.LoadLastThingSaid01 ();
                LoadInformation.GetStartDate ();
                UserInformation.GetDaysPassed();
                if (KITT_IsPlayingAudioRecording == false && KITT_IsRecordingAudio == false) {
                    //Check if it Is Michael And Surveillance Mode Is OFF And That Shut Up Mode Is OFF
                    if (KITTmodesObject.isMichael == true && KITTmodesObject.SurveilanceModeActive == false
                       && KITTmodesObject.KITT_isSpeaking == false && KITTmodesObject.ShutUpMode == false
                       && KITTmodesObject.KITT_SaidThis == false) {
                        if ((lastQorA == "AreYouAspaceship" || UserInformation.LastThingSaid01 == "AreYouAspaceship")
                            && (UserInformation.startDate > UserInformation.today)){
                            KITTvoiceBox.YouHadAlreadyAskedThatResp ();
                            //Negative So Subtract 1 Point From The Mood Level
                            KITTmodesObject.KITT_MoodLevel--;
                            Debug.Log ("Why Is Michael Asking Me This Again? That Is Most Anoying");
                        } else if (KITTmodesObject.isMichael == true && KITTmodesObject.SurveilanceModeActive == false
                                  && KITTmodesObject.KITT_isSpeaking == false && KITTmodesObject.ShutUpMode == false
                                  && KITTmodesObject.KITT_SaidThis == false) {
                            Debug.Log ("Michael Asked ---- Are You A Spaceship");
                            KITTvoiceBox.AreYouAspaceshipResp ();
                            //Negative So Subtract "1" point From The Mood Level
                            //Stating the obvious does not earn any "Brownie Points"
                            KITTmodesObject.KITT_MoodLevel--;
                            lastQorA = "AreYouAspaceship";
                            SaveInformation.SetStartDate ();
                            UserInformation.LastThingSaid01 = ("AreYouAspaceship");
                            SaveInformation.SaveLastThingSaid01();

                            print ("I Will Remember This");
                        }
                    }

I meant, that by removing the .ToString() from the UserInformation.GetDaysPassed() function, you are able to compare the return value in your main script. In it’s current form, your UserInformation.GetDaysPassed() returns a String and not a value that can be compared using operators other than not-equals-to and equals-to.

double daysPassed;

void Start()
{
     daysPassed = UserInformation.GetDaysPassed();
 }

void OnGUI()
{
     if(daysPassed == 0)
     {
          GUILayout.Label("You started today.");
     }
     else if(daysPassed > 0)
     {
          GUILayout.Label("Days Passed : " + daysPassed.ToString("0");
     }
}
1 Like

thanks @TBranflakes
I don’t really want to use the OnGUI at all I just have it there temporarily to test… but on start up it spits out some insane number I don’t know where it’s getting that from… Bear with me… i’ll try to post my scripts

I have a SaveInformation Script

using UnityEngine;
using System.Collections;

public class SaveInformation {

    public static void SaveLastThingSaid01(){
        PlayerPrefs.SetString ("lastQorA01", UserInformation.LastThingSaid01);
    }
    public static void SaveLastThingSaid02(){
        PlayerPrefs.SetString ("lastQorA02", UserInformation.LastThingSaid02);
    }



    /*public static void SetStartDate()
    {
        UserInformation.startDate = System.DateTime.Now ; //save the start date ->
        PlayerPrefs.SetString("DateAsked", UserInformation.startDate.ToString()) ;
        Debug.Log ("Setting Start Date Now");
    }*/

    public static void SetStartDate()
    {
        if(PlayerPrefs.HasKey("DateAsked")) //if we have the start date saved, we'll use that
            UserInformation.startDate = System.Convert.ToDateTime(PlayerPrefs.GetString("DateAsked")) ;
        else //otherwise...
        {
            UserInformation.startDate = System.DateTime.Now ; //save the start date ->
            PlayerPrefs.SetString("DateAsked", UserInformation.startDate.ToString()) ;
        }
    }
}

And A LoadInformation Script

using UnityEngine;
using System.Collections;

public class LoadInformation {

    public static void LoadLastThingSaid01(){
        UserInformation.LastThingSaid01 = PlayerPrefs.GetString ("lastQorA01");
    }
    public static void LoadLastThingSaid02(){
        UserInformation.LastThingSaid02 = PlayerPrefs.GetString ("lastQorA02");
    }


    public static void GetStartDate()
    {
        UserInformation.startDate.ToString( PlayerPrefs.GetString ("DateAsked"));

        Debug.Log("I Got The Start Date Michael");
        Debug.Log (UserInformation.startDate.ToString());
    }
}

Then my UserInformation Script

using UnityEngine;
using System.Collections;

public class UserInformation : MonoBehaviour {

    public static string LastThingSaid01{
        get; set;
    }

    public static string LastThingSaid02{
        get; set;
    }

    public static System.DateTime startDate;
    public static System.DateTime today;

    void Start(){
        LoadInformation.GetStartDate ();
    }

    /*public static void SetStartDate()
    {
        startDate = System.DateTime.Now ; //save the start date ->
        PlayerPrefs.SetString("DateInitialized", startDate.ToString()) ;
        print ("Setting Start Date Now");
    }*/

    public static string GetDaysPassed()
    {
        today = System.DateTime.Now ;

        //days between today and start date -->
        System.TimeSpan elapsed = today.Subtract(startDate) ;

        double days = elapsed.TotalDays ;

        Debug.Log ("It Was this Manys Days Since you Asked Me That Last " + days.ToString("0"));


        return days.ToString("0") ;

    }
}

I uploaded a screen grab of the insane number the onGUI spits out Vs what I think it should spit out on startup which I would have thought was my public static void SetStartDate ()
But maybe i’m doing something wrong in how I’m saving and loading it maybe??

2908040--214286--Screenshot 2017-01-04 15.12.05.png

It’s also in this specific part of my if statement I want to be able to cross reference the:
UserInformation.startDate > UserInformation.today
I thought I might have ben on the right track with what I was attempting there to compare the current date with my saved date fro when the last time the question was asked.

if ((lastQorA == "AreYouAspaceship" || UserInformation.LastThingSaid01 == "AreYouAspaceship")
                            && ([B]UserInformation.startDate > UserInformation.today[/B])){
                            KITTvoiceBox.YouHadAlreadyAskedThatResp ();

Just by taking a quick glance at your code again it appears that in your UserInformation script, you’re only assigning the today variable within the GetDaysPassed function, and you’re only assigning startDate when the GetStartDate function is called. Therefore, calling UserInformation.startDate and UserInformation.today will always return null until after those functions are called.

As far as getting that random number, I’m not sure. I don’t typcically deal with using double values in my personal coding. I prefer floats/ints. :stuck_out_tongue:

using UnityEngine;
using System.Collections;
 
public class UserInformation : MonoBehaviour {
 
    public static string LastThingSaid01{
        get; set;
    }
 
    public static string LastThingSaid02{
        get; set;
    }
 
    public static System.DateTime startDate;
    public static System.DateTime today;
 
 
 
    /*public static void SetStartDate()
    {
        startDate = System.DateTime.Now ; //save the start date ->
        PlayerPrefs.SetString("DateInitialized", startDate.ToString()) ;
        print ("Setting Start Date Now");
    }*/
 
    public static string GetDaysPassed()
    {
        today = System.DateTime.Now ;
 
        //days between today and start date -->
        System.TimeSpan elapsed = today.Subtract(startDate) ;
 
        double days = elapsed.TotalDays ;
 
        return days.ToString("0") ;
    }
}
1 Like

Thanks @TBranflakes,
I’m still not certain on how to go about getting this to do quite what I want it to do?
It seems like when My script reaches the save part of the Switch statement then the OnGUI stuff changes to zero.
I put some debugs in my code and as near as I can tell the player prefs is saving and loading my date the question was asked: (See Attached)

But why on start up of my game session the OnGUI is not showing the days passed based on it’s equation I just don’t get.
All I want to do is find out if the number of days since I asked the question is “One Day” and if atleast one day has fully passed since I asked that question ignore the first part of the if statement where he says “You Already Asked Me That” and go onto the 2nd part where it gives a different answer and then saves the new date time in order to start the process over again… sounds simple enough but this blasted script Is driving me nuts… been at it 4 days now.

2909011--214439--Screenshot 2017-01-04 10.34.16.png

1 Like

It sounds like you may just need to do some heavy debugging on accessing the days passed function. Try creating a small temporary debugging script that uses the GetDaysPassed button, but rather than getting the PlayerPrefs or worrying about load/saving, just use public variables that can be modified in the inspector.

Without being able to open up the project to run it myself, I can only speculate what may be the issue.

1 Like

Thanks @TBranflakes,
I did put Debugs pretty much everywhere… I kinda think of them as KITT’s thoughts lol
If you look at the attached you can see that it’s loading in the saved date the question was asked but just below it it still spits out that ridiculous number in the OnGUI crap.

2909168--214449--Screenshot 2017-01-05 10.44.07.png

Also in my load information I have this Debug response.

2909178--214450--Screenshot 2017-01-05 10.49.38.png

Sooooo… as near as I can tell from the Debugs it is saving and loading my date’s but why I can’t get that into usable data for my if statement is my real issue & why that OnGUI is spitting out that “736333” number… I don’t get that.
This is my entire case statement in the bladed part is where I want to check for if the date is at least one day past before moving onto the 2nd part of the if statement. I thought I was onto something with that
UserInformation.startDate > UserInformation.today

case "AreYouAspaceship":
                LoadInformation.LoadLastThingSaid01 ();
                LoadInformation.GetStartDate ();
                UserInformation.GetDaysPassed();
                if (KITT_IsPlayingAudioRecording == false && KITT_IsRecordingAudio == false) {
                    //Check if it Is Michael And Surveillance Mode Is OFF And That Shut Up Mode Is OFF
                    if (KITTmodesObject.isMichael == true && KITTmodesObject.SurveilanceModeActive == false
                       && KITTmodesObject.KITT_isSpeaking == false && KITTmodesObject.ShutUpMode == false
                       && KITTmodesObject.KITT_SaidThis == false) {
                        if ((lastQorA == "AreYouAspaceship" || UserInformation.LastThingSaid01 == "AreYouAspaceship")
                            && ([B]UserInformation.startDate > UserInformation.today[/B])){
                            KITTvoiceBox.YouHadAlreadyAskedThatResp ();
                            //Negative So Subtract 1 Point From The Mood Level
                            KITTmodesObject.KITT_MoodLevel--;
                            Debug.Log ("Why Is Michael Asking Me This Again? That Is Most Anoying");
                        } else if (KITTmodesObject.isMichael == true && KITTmodesObject.SurveilanceModeActive == false
                                  && KITTmodesObject.KITT_isSpeaking == false && KITTmodesObject.ShutUpMode == false
                                  && KITTmodesObject.KITT_SaidThis == false) {
                            Debug.Log ("Michael Asked ---- Are You A Spaceship");
                            KITTvoiceBox.AreYouAspaceshipResp ();
                            //Negative So Subtract "1" point From The Mood Level
                            //Stating the obvious does not earn any "Brownie Points"
                            KITTmodesObject.KITT_MoodLevel--;
                            lastQorA = "AreYouAspaceship";
                            SaveInformation.SetStartDate ();
                            UserInformation.LastThingSaid01 = ("AreYouAspaceship");
                            SaveInformation.SaveLastThingSaid01();

                            print ("I Will Remember This");
                        }
                    }

Hmm…comment out your GetDaysPassed function and try this…

 public string GetDaysPassed()
{
     today = System.DateTime.Now;

     System.TimeSpan difference;

     difference = today - startDate;

     Debug.Log("Days Passed: " + difference.Days);

     return difference.Days.ToString();
}

And see what your return value is.

1 Like

Thanks @TBranflakes,
Unable to test that as it throws these errors:

2909217--214454--Screenshot 2017-01-05 11.13.49.png

Oops I had to add the “static” in there… but I still get this:

2909222--214456--Screenshot 2017-01-05 11.16.29.png

But if I ask the question it sets the OnGUI to “0” ??? WTF??

It should set it to 1 0r 0 depending on days passed I would have thought?

2909227--214457--Screenshot 2017-01-05 11.21.42.png

For what it’s worth, here is with the VR command.

Okay so I’m starting to believe that the problem is actually the start date. Try the code below. You should return 6 days difference. If this works, then the problem is the startDate.

 public static string GetDaysPassed()
{
     today = System.DateTime.Now;

     Date compareDate = today.AddDays(6);

     System.TimeSpan difference;

     difference = compareDate - today;

     Debug.Log("Days Passed: " + difference.Days);

     return difference.Days.ToString();
}
1 Like

And it’s pulling that Days passed “0” from here after I have asked the question a 2nd time.???

@TBranflakes,

It throws back this error:

2909237--214460--Screenshot 2017-01-05 11.29.25.png