c# - boolean 值始终相同

标签 c# unity-game-engine boolean

我正在尝试在一个简单的统一游戏中设置玩家回合,但是我的玩家回合 boolean 值(这是一个两人游戏)似乎根本没有改变。玩家转 boolean 值的值对于我的统一脚本中的代码很重要,因此是依赖的,但是无论我做出什么更改,它都坚持一个值。我已经搜索了答案,但没有找到适合我的上下文的答案,因此我将在这里转储我的问题。

代码如下:

if (TurnP1 = true)
{ 
    TurnP1 = false;
}
else
{
    TurnP1 = true;  
}
DropPiece(columnNumber, TurnP1);
TestForWinner();
TestForDraw();

非常感谢您的帮助。

最佳答案

问题是你在这里实际上做了一个作业:

if (TurnP1 = true)

它与:

相同
TurnP1 = true;
if (TurnP1) {
    TurnP1 = false;
} else {
    TurnP1 = true;
}

此条件始终为true
您需要将此代码更改为

if (TurnP1 == true)

但是,所有这些代码行都可以替换为否定:

TurnP1 = !TurnP1;

DropPiece(columnNumber, TurnP1);
TestForWinner();
TestForDraw();

关于c# - boolean 值始终相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33643669/

相关文章:

android - 加力无方向

java - 在 Java 中如何使 false 等于 true

java - 将 MySQL DB 对象与 Java 中的字符串进行比较

c# - 编程 "Lean left and Lean right"- Kinect 中的手势

c# - LINQ to SQL 和 Null 字符串,如何使用 Contains?

c# - 在 C# 中使用 Selenium RemoteWebDriver

c# - 为什么 TempData 由 Session 支持

unity-game-engine - Unity3D 部分网格在某些角度下变得不可见

android - Unity Render.TransparentGeometry 性能问题

c# - 将 ToggleButton 绑定(bind)到 boolean 值