Oh … I got my own answer.
Just add a null check on my getter.
public HashSet<string> ChangeMyString
{
get
{
if (MyString == null)
{
MyString = new HashSet<string>();
}
return MyString;
}
set
{
MyString = value;
}
}
Don’t add a new hashset on mouse click, because it will remove the entire hashset and add a new value. Below is a bad code to add hashset. That’s why use getter and add a null check.
private void Update()
{
if (Input.GetMouseButtonDown(0)) //Try Store string value of datetime to MyString (HashSet) On left Click
{
System.DateTime theTime = System.DateTime.Now;
string datetime = theTime.ToString("yyyy-MM-dd-HH-mm-ss");
ChangeMyString = new HashSet<string>();
ChangeMyString.Add(datetime);
Debug.Log($"DOes the {datetime} exist on? {MyString.Contains(datetime)}");
}
CLICK HERE to find out more related problems solutions.