C# 非发票会员';存储系统&x27;不能像方法一样使用
我在unity中设置了一个保存和加载系统,但是我现在在尝试调用必要的函数时遇到了这个错误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
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
的调用。