c# - 无法将带有 [] 的索引应用于“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这是我用来参考的视频
解决方案
您可能打算将元素添加DiceValues
到DiceTotal
:
Dicetotal += DiceValues [i];
// Here -----^
推荐阅读
- javascript - 画布内的工具提示定位
- flutter - 如何在 Flutter 中构建自定义图像提供程序?
- assembly - ASM 反汇编 RCR 函数调用
- node.js - 每 2 小时使用 bash 脚本重新启动 node.js 脚本
- wireguard - wireguard 只能连接到家庭子网(raspberrypi)
- c# - 如何使用多个 XPath 查询在 c# 中选择单个 XML 节点
- vue.js - Vuex 商店:当我尝试将新对象添加到数组时,会导致错误为“检测到重复键:'test1'。这可能会导致更新错误。”
- c - 在 Windows 上的 C 中使用 curl
- python - 在Unittest中用setUp和tearDown替换python with_statement
- mysql - 在 varchar 列上使用 ORDER BY 时如何优化查询?