【发布时间】:2017-03-12 15:22:31
【问题描述】:
我试图在GameView 中显示最好的分数,但我的做法不起作用。
我有一名球员必须避开障碍物,但当他未能避开障碍物时,他将无法再移动并且计分将终止。然后,我想把那个特定的分数添加到我的List。
但是,在我的代码中,没有添加分数,因为每当我开始游戏时,我都会收到 "Argument out of range" 错误,如果我运行 Debug.Log,我可以看到我的列表中没有任何项目。
这是我的代码。 (在这段代码中,我只想打印第一个索引上的分数,稍后我会添加if conditions 以获得真正的最佳分数)。您应该主要关注void Start() 和void Update() 的前几行。
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class ScoreManager : MonoBehaviour {
private float score = 0.0f;
private int difficultyLevel = 1;
private int scoreIncrementor = 1;
private int maxdifficultyLevel = 10;
private int scoreToNextLevel = 10;
private bool isDead = false;
private List<float> scoreBox;
public Text scoreText;
public Text bestScoreText;
void Start(){
scoreBox = new List<float> ();
for(float i = 0; i <= scoreBox.Count; i++)
bestScoreText.text = ("Best Score: " + ((int)scoreBox [0]).ToString ());
}
void Update () {
if (isDead) {
scoreBox.Add (score);
return;
}
if (score >= scoreToNextLevel)
LevelUp ();
score += Time.deltaTime;
scoreText.text = ("Score: " + " "+ ((int)score).ToString ());
}
void LevelUp(){
if (difficultyLevel == maxdifficultyLevel)
return;
scoreToNextLevel *= 2;
difficultyLevel++;
GetComponent<PlayerMovement> ().SetSpeed (scoreIncrementor);
}
public void OnDeath(){
isDead = true;
}
}
【问题讨论】: