Sei*_*eld 0 c# list unity-game-engine
我试图在a中显示最佳分数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;
}
}
Run Code Online (Sandbox Code Playgroud)
问题在于该Start()方法.
void Start(){
scoreBox = new List<float> ();
for(float i = 0; i <= scoreBox.Count; i++)
bestScoreText.text = ("Best Score: " + ((int)scoreBox [0]).ToString ());
}
Run Code Online (Sandbox Code Playgroud)
您正在创建一个没有元素的新列表,然后您尝试显示结果:但是没有位置0的元素存在,因此您得到了一个IndexOutOfRange例外.
更改<=为<(记住,索引从0开始.但长度从1开始)scoreBox[0]应该是scoreBox[i].
另外,我可以问你为什么要把这个列表当作float你要投的那个int?
| 归档时间: |
|
| 查看次数: |
968 次 |
| 最近记录: |