首页 > 解决方案 > 无法将带有 [] 的索引应用于“int”类型的表达式

问题描述

为了被我想做的研究接受,我需要制作一个游戏原型。为此,我为我的电脑购买了一些升级,以便它可以运行 Unity(所以我是第一次使用它)

我正在通过以下视频指南了解一切是如何工作的。我目前正在按照指南在 Unity 中制作棋盘游戏。

我完全复制了视频显示的内容。在视频中一切顺利。但是,我收到以下错误。

错误 CS0021:无法使用 [] 将索引应用于“int”类型的表达式

我对编码很陌生,并且尝试了多种方法来解决它,但我不知道出了什么问题。

public class DiceRoller : MonoBehaviour {

// Use this for initialization
void Start () {
    DiceValues = new int[2];
}

// Update is called once per frame
void Update () {

}

public int[] DiceValues;
public int Dicetotal;

public void RollTheDice() {

    Dicetotal = 0;
    for (int i = 0; i < DiceValues.Length; i++)
    {
        DiceValues [i] = Random.Range ( 0, 5 );
        Dicetotal += Dicetotal [i]; 
    }

    Debug.Log ("Rolled: " + DiceValues + " (" + Dicetotal + ")");
}

https://www.youtube.com/watch?v=8rCSED1c8NU&t=1313s这是我用来参考的视频

标签: c#unity3d

解决方案


您可能打算将元素添加DiceValuesDiceTotal

Dicetotal += DiceValues [i]; 
// Here -----^

推荐阅读