简体   繁体   English

Unity3D - Integer 列表值未在 Class 的构造函数中初始化

[英]Unity3D - Integer List values don't get initialized in Constructor of Class

In my Unity3D Project I made a simple 2D Game and implemented a Data Manager to save and load Data.在我的 Unity3D 项目中,我制作了一个简单的 2D 游戏并实现了一个数据管理器来保存和加载数据。 I created a List but get a NullReferenceException whenever I try to refer to it.我创建了一个列表,但每当我尝试引用它时都会得到一个 NullReferenceException。 I have a DataManager Class (saves & loads Data) and a UserData Class (stores the fields that are needed to be saved or loaded).我有一个 DataManager Class(保存和加载数据)和一个 UserData Class(存储需要保存或加载的字段)。 In the User Data Class I declare in initialize a List of integer type.在用户数据 Class 中,我声明初始化一个 integer 类型的列表。 The List contains the unlocked Levels and it is initialized in the Constructor of the User Data Class.该列表包含解锁级别,并在用户数据 Class 的构造函数中初始化。

This is the DataManager Class:这是 DataManager Class:

public static class DataManager
{
    public static List<int> GetUnlockedLevels()
    {
        UserData userData = Load();
        return userData.unlockedLevels; // This method returns nothing, not even null!
    }

    private static void Save(UserData data)
    {
        string path = GetDataFilePath();
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        using (FileStream fileStream = File.Open(path, FileMode.OpenOrCreate))
        {
            binaryFormatter.Serialize(fileStream, data);
        }
    }

    private static UserData Load()
    {
        string path = GetDataFilePath();
        if (!File.Exists(path))
        {
            UserData userData = new UserData();
            Save(userData); 
        }
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        using (FileStream fileStream = File.Open(path, FileMode.Open))
        {
            return (UserData)binaryFormatter.Deserialize(fileStream);
        }
    }

And here comes the UserData Class: UserData Class 来了:

[Serializable]
public class UserData
{
    public int score;
    public List<int> unlockedLevels;
    public UserData()
    {
    score = 50;
    unlockedLevels = new List<int>();
    unlockedLevels.Add(1); //unlocked by default
    unlockedLevels.Add(2); //unlocked by default
    unlockedLevels.Add(3); //unlocked by default
    }
}

The problem is: the first method of the DataManager "GetUnlockedLevels()" returns nothing.问题是:DataManager“GetUnlockedLevels()”的第一个方法什么都不返回。

The weird part: I have the exact same Data Manager in another project where it works properly.奇怪的部分:我在另一个正常工作的项目中有完全相同的数据管理器。 In that other project, the GetUnlockedLevels-method returns "System.Collections.Generic.List´1[System.Int32]" when I return it via "Debug.Log".在另一个项目中,当我通过“Debug.Log”返回时,GetUnlockedLevels 方法返回“System.Collections.Generic.List´1[System.Int32]”。 But in the new project, the method returns literally nothing (not even null; the exception comes at a later point) I am sure that I didn't make a copy-paste-mistake.但是在新项目中,该方法几乎没有返回任何内容(甚至没有返回 null;异常稍后出现)我确信我没有犯复制粘贴错误。 What could be the root for this error?这个错误的根源是什么?

What IDE are you using?你用的是什么IDE? I ask because the answer to your question is that you have a typo in your UserData constructor.我问是因为你的问题的答案是你的 UserData 构造函数中有错字。 Normally you would be alerted to this as soon as you built within your IDE as it would fail to compile.通常,一旦您在 IDE 中构建,您就会收到警告,因为它无法编译。

[Serializable]
public class UserData
{
    public int score;
    public List<int> unlockedlevels; // This line
    public UserData()
    {
    score = 50;
    unlockedLevels = new List<int>(); // And this line
    unlockedLevels.Add(1); // And this line
    unlockedLevels.Add(2); // And this line
    unlockedLevels.Add(3); // And this line
    }
}

unlocked L evels解锁关卡

vs对比

unlocked l evels解锁水平

Solution: the file was created but was empty.解决方案:文件已创建但为空。 I manually deleted the saved file and from then it worked properly.我手动删除了保存的文件,然后它就可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM