C# 错误CS0029:无法隐式转换类型';int';至';得分';
我已经搜索了一段时间,但找不到答案,希望有人能帮助我!。我试图做的是保存我的分数并将其转移到另一个场景。使用这里的代码,我得到了错误: 错误CS0029:无法将类型“int”隐式转换为“Score” 我对unity脚本也是相当陌生的 下面是我正在使用的两个脚本 脚本1 Score.csC# 错误CS0029:无法隐式转换类型';int';至';得分';,c#,unity3d,int,C#,Unity3d,Int,我已经搜索了一段时间,但找不到答案,希望有人能帮助我!。我试图做的是保存我的分数并将其转移到另一个场景。使用这里的代码,我得到了错误: 错误CS0029:无法将类型“int”隐式转换为“Score” 我对unity脚本也是相当陌生的 下面是我正在使用的两个脚本 脚本1 Score.cs using UnityEngine; using System.Collections; public class Score : MonoBehaviour { static public int
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;
}
}
非常感谢您的帮助
PLayerPrefs.GetInt
将返回一个int
,但是您将一个Score
类克隆指定
PLayerPrefs.GetInt
对于它,int
不能成为Score
,因此如果您想访问Score类变量,您应该这样做
void Start () {
score=score.GetComponent<Score>();
score.score = PlayerPrefs.GetInt ("score");
}
PLayerPrefs.GetInt
将返回一个int
,但您将放置一个Score
类克隆来指定
PLayerPrefs.GetInt
对于它,int
不能成为Score
,因此如果您想访问Score类变量,您应该这样做
void Start () {
score=score.GetComponent<Score>();
score.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");
错误是由上述行引起的PlayerPrefs.GetInt
作为其名称状态将返回一个整数。现在看看如何声明score
变量:
Score score;
这导致变量score
具有类型score
类,而不是int
我想您想在score
类中设置变量score
。由于您将变量score
声明为static public
,这使事情变得简单。您不必创建Score
的实例,只需使用类名Score
(大写S):
Score.score = PlayerPrefs.GetInt("score");
您不能在代码段工具中运行C代码。它用于javascript、html和css。是的,请不要对非JS/html/css代码使用堆栈代码段,我已经删除了它。您不能在代码段工具中运行C代码。它是针对javascript、html和css的。是的,请不要对非JS/html/css代码使用堆栈片段,我已经删除了它。好的,谢谢你的帮助,我做到了你说的。我不再犯错误了,这是一件好事!。但是在死亡场景中,它没有显示分数为“0”的分数。你知道它为什么这样做吗?试着在
ondestory()
中打印Score
的值,我们将查看该函数是否被调用,分数是否正确递增。在调用AddPointI did Debug.Log(分数)的地方;在void OnDestroy()下,它给出了一个数字。现在请尝试在Start()
中打印Score.Score
和Update()
方法GetScore
脚本。好的,谢谢你的帮助,我完成了你说的任务。我不再犯错误了,这是一件好事!。但是在死亡场景中,它没有显示分数为“0”的分数。你知道它为什么这样做吗?试着在ondestory()
中打印Score
的值,我们将查看该函数是否被调用,分数是否正确递增。在调用AddPointI did Debug.Log(分数)的地方;在void OnDestroy()下,它给出了一个数字。现在,请尝试在Start()
和Update()中打印Score.Score
脚本的method。