错误CS0029:无法将类型'int'隐式转换为'Score'

时间:2014-09-25 07:30:16

标签: c# unity3d int

我已经搜索了一段时间,无法找到并回答希望有人可以帮助我!我想要做的是保存我的分数并将其转移到另一个场景。使用此代码我在这里得到错误:

  

错误CS0029:Cannt隐式将类型'int'转换为'Score'

我对团结剧本也很陌生。

以下是我正在使用的两个脚本

脚本1 Score.cs

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {

    static public int score = 0;
    static public int highScore = 0;

    static Score instance;

    static public void AddPoint() {
        if(instance.run.dead)
            return;

        score++;

        if(score > highScore) {
            highScore = score;
        }
    }
    Running run;

    void Start() {
        instance = this;
        GameObject player_go = GameObject.FindGameObjectWithTag("Player");
        if (player_go == null) {
            Debug.LogError("could not find an object with tag 'Player'.");
        }
        run = player_go.GetComponent<Running> ();
        score = 0;
    }

    void OnDestroy() {
        PlayerPrefs.SetInt ("score", score);
    }

    void Update () {
        guiText.text = "Score: " + score;
    }
}

以及将其送到另一个场景的第二个脚本

using UnityEngine;
using System.Collections;

public class GetScore : MonoBehaviour {

    Score score;

    // Use this for initialization
    void Start () {
        score = PlayerPrefs.GetInt ("score");

    }

    // Update is called once per frame
    void Update () {
        guiText.text = "Score: " + score;

    }
}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

score = PlayerPrefs.GetInt ("score");

错误是由上面的行引起的。 PlayerPrefs.GetInt因为它的名称状态将返回一个整数。现在看看你如何声明score变量:

Score score;

这会导致变量score的类型为Score,而不是int

我想你想在score类中设置变量Score。由于您将变量score声明为static public,因此简化了操作。您不必创建Score的实例,只需使用类名Score (使用大写S)

Score.score = PlayerPrefs.GetInt("score");

答案 1 :(得分:2)

PLayerPrefs.GetInt将返回int,但您放置Score类克隆以指定返回值 PLayerPrefs.GetInt intScore无法成为void Start () { score=score.GetComponent<Score>(); score.score = PlayerPrefs.GetInt ("score"); } 所以如果你想访问得分类变量,你应该这样做

static

因为您的分数可变 void Start () { Score.score = PlayerPrefs.GetInt ("score"); } ,您也可以使用

{{1}}
相关问题