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
C# 将分数从其他脚本保存到savescript_C#_Unity3d - Fatal编程技术网

C# 将分数从其他脚本保存到savescript

C# 将分数从其他脚本保存到savescript,c#,unity3d,C#,Unity3d,我试图在关卡结束时保存分数,但我遇到了一些错误 下面是评分脚本: using UnityEngine; using System.Collections; public class ScoreManager : MonoBehaviour { public float score; private IEnumerator Wait() { yield return new WaitForSeconds(3); Application.LoadL

我试图在关卡结束时保存分数,但我遇到了一些错误

下面是评分脚本:

using UnityEngine;
using System.Collections;

public class ScoreManager : MonoBehaviour {
    public float score;

    private IEnumerator Wait() {
        yield return new WaitForSeconds(3);
        Application.LoadLevel(Application.loadedLevel);
    }

    void TimerOfDeath(){
        if(score <= 0){
            GameObject.Find("TooLateGUI").guiTexture.enabled = true;
            GameObject.Find("Score").guiText.enabled = false;
            StartCoroutine(Wait());


        }
    }

    void Update () {
            {
            score -= 60 * Time.deltaTime;
            guiText.text = "Score: " + (int) score;
            TimerOfDeath ();
        }
    }
}

第一个相当简单,您可以在这行中将变量声明为GameObject:

GameObject Score;
当您实际想要存储ScoreManager时。您需要将其更改为:

ScoreManager Score;
第二个问题是通过改变

int newScore = (int)ScoreManager.score; //get score and put in newScore as int

因为“ScoreManager”是类的名称,而您使用的实例名为“Score”。也许可以看看什么是静态函数;)我还建议您将您的分数变量重命名为一个可以清楚表明它实际上是一个ScoreManager的变量。我通常只使用

ScoreManager scoreManager;


请注意,实例名称通常以小写字符开头,类以大写字符开头。这就是为什么在代码中突出显示了“分数”,stackoverflow认为它是一个类,而实际上它是一个实例名

感谢mate修复了构建错误现在我仍然要找出如何将分数添加到数组并显示所有级别的最高分数,但我今天比我想象的还要感谢Againp,试试看,直到你走不动为止,然后再回来:)你好,汤姆,我从昨天开始走得有点远了,但我不确定我做得是否正确,以及如何填补最后的漏洞。关于这个问题,我问了一个新问题,你介意看一下吗?
int newScore = (int)ScoreManager.score; //get score and put in newScore as int
int newScore = (int)Score.score; //get score and put in newScore as int
ScoreManager scoreManager;
ScoreManager myScoreManager;