简体   繁体   English

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

[英]error CS0029: Cannot implicitly convert type 'int' to 'Score'

I have searched for a while and can't find and answer hoping someone can help me!. 我搜索了一段时间,无法找到答案,希望有人可以帮助我! What I am trying to do is save my Score and transfer it to a different scene. 我想做的是保存我的分数并将其转移到另一个场景。 With this code I have here I get the error : 有了这段代码,我在这里得到错误:

error CS0029: Cannt implicitly convert type 'int' To 'Score' 错误CS0029:无法将类型'int'隐式转换为'Score'

I am rather new to unity script as well. 我对统一脚本也很陌生。

Here are the two scripts I am using 这是我正在使用的两个脚本

Script 1 Score.cs 脚本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;
    }
}

and the second script to get it to the other scene 第二个脚本将其转到另一个场景

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;

    }
}

Really appreciate any help! 非常感谢任何帮助!

score = PlayerPrefs.GetInt ("score");

The error is caused by above line. 该错误是由上述行引起的。 PlayerPrefs.GetInt as it name states will return an integer. 名称说明中的PlayerPrefs.GetInt将返回一个整数。 And now see how you declare the score variable: 现在看看如何声明score变量:

Score score;

This causes the variable score have a type Score class, not int . 这导致变量score具有类型Score类,而不是int类型。

I suppose you want to set the variable score in Score class. 我想您想在Score类中设置变量score Since you declare the variable score as static public , that makes things easy. 由于您将可变score声明为static public ,因此事情变得容易。 You don't have to create an instance of Score , just use the class name Score (with capital S) instead: 您不必创建Score的实例,只需使用类名称Score (大写S)代替:

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

PLayerPrefs.GetInt will return an int but you put a Score class clone to assign the returning value of PLayerPrefs.GetInt to it , int cant become Score so if you want to access the score class variable you should do this PLayerPrefs.GetInt将返回一个int但您放置了一个Score类克隆以将PLayerPrefs.GetInt的返回值分配给它,因此int不能成为Score因此如果您要访问score类变量,则应执行此操作

void Start () {
    score=score.GetComponent<Score>();
    score.score = PlayerPrefs.GetInt ("score");

}

because your score varaible is static you can use this too 因为您的分数可变性是static您也可以使用它

 void Start () {

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

}

暂无
暂无

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

相关问题 错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor) - Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor) 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string' CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39; - CS0029: Cannot implicitly convert type 'int' to 'bool' 错误 CS0029 无法将类型“字符串”隐式转换为“双精度” - Error CS0029 Cannot implicitly convert type 'string' to 'double' 错误CS0029无法将类型“字符串”隐式转换为“十进制” - Error CS0029 Cannot implicitly convert type 'string' to 'decimal' 编译器错误消息:CS0029:无法将类型“int”隐式转换为“string”错误 - Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string' error 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39;-Unity3D - error CS0029: Cannot implicitly convert type `int' to `bool' - Unity3D 编译器错误消息:CS0029:无法将类型“int”隐式转换为“string” - Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string' C# - 错误 CS0029 无法将类型“int”隐式转换为“Core.Models.Mandate” - C# - Error CS0029 Cannot implicitly convert type 'int' to 'Core.Models.Mandate'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM