Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.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
C# 非发票会员';存储系统&x27;不能像方法一样使用_C#_Unity3d - Fatal编程技术网

C# 非发票会员';存储系统&x27;不能像方法一样使用

C# 非发票会员';存储系统&x27;不能像方法一样使用,c#,unity3d,C#,Unity3d,我在unity中设置了一个保存和加载系统,但是我现在在尝试调用必要的函数时遇到了这个错误 public class SaveSystem { public void Save() { AllData allData = new AllData(); BinaryFormatter bf = new BinaryFormatter(); FileStream file = new FileStream(Application.per

我在unity中设置了一个保存和加载系统,但是我现在在尝试调用必要的函数时遇到了这个错误

public class SaveSystem
{ 
    public void Save()
    {
        AllData allData = new AllData();
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = new FileStream(Application.persistentDataPath + "/allData.hey", 
            FileMode.Create, FileAccess.Write);
        bf.Serialize(file, allData);
        file.Close();
    }

    public void Load()
    {
        AllData allData = new AllData();

        if (File.Exists(Application.persistentDataPath + "/allData.hey"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/allData.hey", 
                FileMode.Open, FileAccess.Read);
            allData = (AllData)bf.Deserialize(file);

            file.Close();
        }
    }
}

public class Shop : MonoBehaviour
{
    AllData allData = new AllData();

    public Start(){
        saveSystem.Load();
     }

     public Update(){
        saveSystem.Load();
      }
}

我认为您在店铺行为中遗漏了saveSystem的会员声明:

public class Shop : MonoBehaviour
{
    private SaveSystem saveSystem = new SaveSystem();
    AllData allData = new AllData();

    public Start(){
        saveSystem.Load();
     }

     public Update(){
        saveSystem.Load();
      }
}
然而,只要看看上面的代码,它就有很多问题。一个是在Shop类中以及Save()和Load()方法中创建AllData()的新实例,因此实际上没有实现任何功能。您应该在Shop对象中保留一个AllData()实例,并将此AllData实例传递给Save()方法,同时设置this.AllData=saveSystem.Load()以重新加载它


祝您好运

该类是
SaveSystem
,但您正在尝试调用
SaveSystem
c#
区分大小写,但错误消息与该问题不匹配;所以我怀疑这不是真正的代码。由于它是一个类,并且没有标记为static,因此需要创建一个实例才能使用任何方法。还有许多其他问题,尤其是
所有数据
被(重新)声明为局部变量,这在某种程度上避免了这些方法问题中的代码不包含对
SaveSystem
SaveSystem
的调用。