[英]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.