Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d Unity NullReferenceException哈希集添加_Unity3d_Nullreferenceexception_Hashset - Fatal编程技术网

Unity3d Unity NullReferenceException哈希集添加

Unity3d Unity NullReferenceException哈希集添加,unity3d,nullreferenceexception,hashset,Unity3d,Nullreferenceexception,Hashset,我想添加hashset,但unity会弹出NullReferenceException。Hashset是删除我的保存游戏所需的非重复值的唯一存储。如何在没有空引用的情况下向hashset字符串添加值 =>当更改字符串.Add(日期时间)时或MyString.Add(datetime)=>我的代码截图:或 我的代码 public class HashsetSource : MonoBehaviour { public HashSet<string> MyString;

我想添加hashset,但unity会弹出NullReferenceException。Hashset是删除我的保存游戏所需的非重复值的唯一存储。如何在没有空引用的情况下向hashset字符串添加值

=>当
更改字符串.Add(日期时间)时
MyString.Add(datetime)=>我的代码截图:或

我的代码

public class HashsetSource : MonoBehaviour
{
    public HashSet<string> MyString;

    public HashSet<string> ChangeMyString
    {
        get
        {
            return MyString;
        }
        set
        {
            MyString = value;
        }
    }

    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.Add(datetime);

            Debug.Log($"DOes the {datetime} exist on? {MyString.Contains(datetime)}");

        }

        if (Input.GetMouseButtonDown(1)) //Try retreive strings on MyString (HashSet) on right click
        {
            foreach (var myString in ChangeMyString)
            {
                Debug.Log(myString);
            }
            
        }

        if (Input.GetMouseButtonDown(2)) //Clear MyString (HashSet) on middle click
        {
            ChangeMyString.Clear();
        }
    }

}
公共类HashsetSource:MonoBehavior
{
公共HashSet-MyString;
公共HashSet ChangeMyString
{
得到
{
返回MyString;
}
设置
{
MyString=值;
}
}
私有void更新()
{
if(Input.GetMouseButtonDown(0))//在左键单击时尝试将datetime的字符串值存储到MyString(HashSet)
{
System.DateTime theTime=System.DateTime.Now;
字符串日期时间=time.ToString(“yyyy-MM-dd-HH-MM-ss”);
ChangeMyString.Add(日期时间);
Debug.Log($“在{MyString.Contains(datetime)}上是否存在{datetime}”);
}
if(Input.GetMouseButtonDown(1))//右键单击MyString(HashSet)尝试检索字符串
{
foreach(ChangeMyString中的var myString)
{
Log(myString);
}
}
if(Input.GetMouseButtonDown(2))//点击鼠标中键清除MyString(HashSet)
{
ChangeMyString.Clear();
}
}
}

哦。。。我有自己的答案

只需在我的getter上添加一个空检查

public HashSet<string> ChangeMyString
{
    get
    {
        if (MyString == null)
        {
            MyString = new HashSet<string>();
        }
        return MyString;
    }
    set
    {
        MyString = value;
    }
}
public HashSet changeystring
{
得到
{
if(MyString==null)
{
MyString=newhashset();
}
返回MyString;
}
设置
{
MyString=值;
}
}
不要在鼠标单击时添加新的哈希集,因为它将删除整个哈希集并添加新值。下面是添加hashset的错误代码。这就是为什么要使用getter并添加空检查

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

        }
private void Update()
{
if(Input.GetMouseButtonDown(0))//在左键单击时尝试将datetime的字符串值存储到MyString(HashSet)
{
System.DateTime theTime=System.DateTime.Now;
字符串日期时间=time.ToString(“yyyy-MM-dd-HH-MM-ss”);
ChangeMyString=newhashset();
ChangeMyString.Add(日期时间);
Debug.Log($“在{MyString.Contains(datetime)}上是否存在{datetime}”);
}