Null string from another script

I’m trying to get the string from the DateTest.cs to DayCheck.cs script, but while doing that the string become null. Is there is any fix?

DateTest.cs

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

public class DateTest : MonoBehaviour
{
    public Text dataText;

    public int iDays = System.DateTime.UtcNow.ToLocalTime().Day;
    public int iMonths = System.DateTime.UtcNow.ToLocalTime().Month;
    public int iYears = System.DateTime.UtcNow.ToLocalTime().Year;
    public int iHour = System.DateTime.UtcNow.ToLocalTime().Hour;
    public int iMinute = System.DateTime.UtcNow.ToLocalTime().Minute;
    // "z" is for the test
    public string days = "z", months, years, hours, minutes, seconds;

    void Update()
    {
        string days = System.DateTime.UtcNow.ToLocalTime().ToString("dd");
        string months = System.DateTime.UtcNow.ToLocalTime().ToString("MM");
        string years = System.DateTime.UtcNow.ToLocalTime().ToString("yyyy");

        string hours = System.DateTime.UtcNow.ToLocalTime().ToString("HH");
        string minutes = System.DateTime.UtcNow.ToLocalTime().ToString("mm");
        string seconds = System.DateTime.UtcNow.ToLocalTime().ToString("ss");

        Debug.Log("days from DT: " + days);

        dataText.text = "Today is " + days + "-" + months + "-" + years + " | " + hours + ":" + minutes + ":" + seconds;
    }
}

DayCheck.cs

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

public class DateTest01 : MonoBehaviour
{
    public GameObject DateTestScript;
    DateTest string1;

public void OnButtonPressed()
    {
        string1 = DateTestScript.GetComponent<DateTest>();

        string cosar = string1.days;
        // Console shows: string1:  (probably null)
        Debug.Log("string1: " + cosar);
    }
}

If DateTest component is already attached to the gameobject at the start or it’s stored in a prefab and instantiated (actually more like cloned) later, then the “days” value will be what is serialized in the Inspector. When setting the value in the class definition (to “z” in this case) will not have any affect.

So it’s not null (you would get null exception otherwise) but just empty (like the probably in the inspector).

So you can set the initial value in the inspector or inside Start or Awake method that are always called automatically when the game starts.

However if you want the “days” variable not be serialized and still accessible from other classes you need to add [System.NonSerialized] attribute before the variable. That way the Inspector will not show the variable and will not set any value to it (meaning you can set it as you like in the class definition).

So for example one working way would to add this to DateTest.cs

void Start() 
{
      days = "z";
}

Other way:

[System.NonSerialized]
public string days = "z";

You redeclared all your variables as local variables inside your Update method. So you never actually assigned any values to your class variables. You should change your Update method to:

void Update()
{
    var time = System.DateTime.UtcNow.ToLocalTime();
    days = time.ToString("dd");
    months = time.ToString("MM");
    years = time.ToString("yyyy");

    hours = time.ToString("HH");
    minutes = time.ToString("mm");
    seconds = time.ToString("ss");
    dataText.text = "Today is " + days + "-" + months + "-" + years + " | " + hours + ":" + minutes + ":" + seconds;
 }

Note that the important thing is that I removed all the string types in front of each line. Now we assign the actual class variables instead of declaring new local variables. I also read the current time once. Reading UtcNow 6 times in a row can result in out of sync reading. So for example around 8:59 you may read an hour value of 8 but a minute value of “00” because at the time the minute reading is happening the time may have advanced already to 9:00.