我创建了一个streamreader和streamwriter来加载和创建文件。但是,我很难在前五个高分列表中进行迭代,以便在实现新分数时更新高分。
public int highscore;
public static int score;
//public new string name;
private string initials;
public List<int> highscores;
//public List<Person> highscores;
Text HighScoreText;
void Awake()
{
HighScoreText = GetComponent<Text>();
score = 0;
highscores = new List<int>();
score = PlayerPrefs.GetInt("Score",0);
highscore = PlayerPrefs.GetInt("HighScore", 0); ;
LoadScore();
UpdateScore();
}
void UpdateScore()
{
HighScoreText.text = "HighScore:" + score;
//Here I get an error cannot apply indexing with type int
if (score > highscores[4].s)
{
highscores.Add(score);
highscores.Sort();
highscore = score;
HighScoreText.text = highscore.ToString(); ;
}
}
答案 0 :(得分:0)
您宣称highscores
为list
int
:public List<int> highscores;
然后您正在检查if (score > highscores[4].s) { ... }
.s
是什么?你根本不需要它。只需检查if (score > highscores[4])
,您的代码就会正常编译。
此外,在编译之后,您可能希望通过调试代码语句来检查逻辑。如果您使用的是Visual Studio,请使用F-10 step-over
选项。