我目前正在C#中的void内部进行迭代。在脚本类内部,我已经声明了整数(名称为selectedLine),但是每当要在void中添加该int时,例如“ selectedLine + = 1” 那么对于迭代中的每个对象,它只会跳到2左右。但是,如果我在void内声明整数,例如“ selectedLine = 0”(请注意,正是之前声明的整数),那么它实际上将在整个迭代过程中相加。
我已经尝试在每个函数开始时将'selectedLine'设置为0。我还尝试过调试,以开始调试(它仍然说0!迭代前整数为0!)
int selectedLine = 0
void TheVoid() {
foreach (GameObject obj in list) {
selectedLine += 1
}
}
// ^ selectedLine = 2 for each obj??
// but if...
int selectedLine = 0
void TheVoid() {
selectedLine = 0
foreach (GameObject obj in list) {
selectedLine += 1
}
}
// ^ Actually adds 1 to the integer for each obj instead of just skipping // to 2 for each.
if I assign the variable inside the void:
1 - 0
2- 1
3 - 2
if I assign the variable outside of the void:
1 - 2
2 - 2
3 - 2