首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"How to fix not saved or loading data“

"How to fix not saved or loading data“
EN

Stack Overflow用户
提问于 2019-05-11 11:47:50
回答 2查看 356关注 0票数 1

因此,我正在为我的蛇游戏使用一个定制器,我正在使用PlayerPrefs来保存蛇的首选外观。现在,每次我测试运行游戏时,playerprefs中保存的字符串并没有加载,它只是空的。

我已经尝试过在Start()、Awake()和Update()方法上获取字符串数据,但它们都没有加载数据。另外,我在保存数据的每一行中都添加了PlayerPrefs.Save()。

这是我的代码,用于更新所有网格,使其与保存的字符串相同。我已经把它放到了Update方法中。

代码语言:javascript
复制
lesnek = GameObject.FindGameObjectsWithTag("LeWorm");

        objName = PlayerPrefs.GetString("meshName");

        if (PlayerPrefs.HasKey("meshName"))
        {
            if (objName == PlayerPrefs.GetString("meshName"))
            {
                leMesh = Resources.Load<GameObject>(@"Models\" + objName);
            }
            else
            {
                objName = PlayerPrefs.GetString("meshName");
                leMesh = Resources.Load<GameObject>(@"Models\" + objName);
            }
        }
        if (objName != null || objName != "")
        {
            objName = "Sphere";
            leMesh = Resources.Load<GameObject>(@"Models\" + objName);
        }

        foreach (GameObject change in lesnek)
        {
            if (objName != null || objName != "")
            {
                if (change.GetComponent<MeshFilter>() == null)
                {
                    change.AddComponent<MeshFilter>();
                    change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh;
                    PlayerPrefs.SetString("meshName", objName);
                    PlayerPrefs.Save();
                }
                else
                {
                    change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh;
                    PlayerPrefs.SetString("meshName", objName);
                    PlayerPrefs.Save();
                }
            }
        }

每次运行这段代码时,我都会收到一个错误,并且没有在playerpref中加载或保存字符串。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-11 19:59:40

这里有一些问题,您的空引用错误是一个主要原因。

我将对您的代码进行注释,以显示您的错误所在,并提供替代解决方案。

代码语言:javascript
复制
// If possible it would be better to assign this object in the inspect rather then 
// using a GameObject.Find method, Also this is error prone due to the timing of 
// the call, for example if this is in a start this Object may not exist yet.
// Another solution would to be create an event and when you load the skin
// Have that event fire off that its been loaded and everything that needs
// to listen to it will get the change.
lesnek = GameObject.FindGameObjectsWithTag("LeWorm");

//  GetString has an alternative method where you can pass a default value
//  I would recommend using this method instead.
objName = PlayerPrefs.GetString("meshName");    // POINT A

// Honestly this check seems a tad bit redundant since you already
// attempted to load this preference.
// POINT B
if (PlayerPrefs.HasKey("meshName"))  // Since you have said this is empty playerPrefs is empty.
{
    // At this point it should equal this since you already set it to this in Point A
    if (objName == PlayerPrefs.GetString("meshName"))
    {
        leMesh = Resources.Load<GameObject>(@"Models\" + objName);
    }
    else
    {
        // This case should never get called, because of Point A and your if condition.
        objName = PlayerPrefs.GetString("meshName");
        leMesh = Resources.Load<GameObject>(@"Models\" + objName);
    }
}

if (objName != null || objName != "")  // This completely nullifies all the work in Point B
{
    objName = "Sphere";
    leMesh = Resources.Load<GameObject>(@"Models\" + objName);
}
foreach (GameObject change in lesnek)
{
    if (objName != null || objName != "")
    {
        if (change.GetComponent<MeshFilter>() == null)
        {
            change.AddComponent<MeshFilter>();
            change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh;
            PlayerPrefs.SetString("meshName", objName);
            PlayerPrefs.Save();
        }
        else
        {
            change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh;
            PlayerPrefs.SetString("meshName", objName);
            PlayerPrefs.Save();
        }
    }
}

无需详细介绍,假设这是一种初始化方法,而不是start,这就是我要做的更改

代码语言:javascript
复制
// This would be better as a class variable rather then a local variable
string meshName = PlayerPrefs.GetString("meshName", "Sphere");  
// Skipping point B because it is redundant
leMesh = Resources.Load<GameObject>(@"Models\" + meshName);  // Is this a GameObject or is it a Mesh???

MeshFilter leMeshFilter = leMesh.GetComponent<MeshFilter>();
if(leMeshFilter != null)
{
    foreach (GameObject change in lesnek)
    {
        MeshFilter meshFilter = change.GetComponent<MeshFilter>();
        if(meshFilter != null)
        {
            meshFilter.mesh = leMeshFilter.sharedMesh;
        }
        else
        {
            meshFilter = change.AddComponent<MeshFilter>();
            meshFilter.mesh = leMeshFilter.sharedMesh;
        }
    }
}

这个例子唯一不做的事情就是保存你的PlayerPreference。这是仅当您更改皮肤时才应该做的事情。因此,无论您在做什么,请保存首选项。

票数 0
EN

Stack Overflow用户

发布于 2019-05-11 17:58:38

未找到游戏对象"leMesh“。第一步是确保它确实被找到了。因此,让我们确保了解它的状态,以便对其进行调试:

代码语言:javascript
复制
if (PlayerPrefs.HasKey("meshName"))
    {
        Debug.Log("PlayerPrefs does have the key we are looking for and it is " + PlayerPrefs.GetString("meshName"));

        /// This is redundant you just set objName to the string earlier
        /// objName = PlayerPrefs.GetString("meshName");
        if (objName == PlayerPrefs.GetString("meshName"))
        {
            leMesh = Resources.Load<GameObject>(@"Models\" + objName);
        }
        else
        {
            /// And here you do it again, constantly trying to get that string
            /// In order to find the object, of course if the string doesnt exist
            /// youll simply get an unassigned referenceException on this line:
            /// change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh
            objName = PlayerPrefs.GetString("meshName");
            leMesh = Resources.Load<GameObject>(@"Models\" + objName);
        }
    }

现在,如果你的控制台日志告诉你meshName返回了一个值,那么你访问的资源是错误的,在这种情况下,你试图从"Models“前面删除"@”。否则,就没有名为meshName的playerPrefs字符串。

ref

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56087012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档