Question by
heroesdavid8 · Jun 12, 2021 at 02:00 PM ·
boolupdate functionbug report
Updating a boolean doesn't work in other classes
In my player class, I have a public variable named inTurn. I have attached a button's onClick function onto the the player class, wanting to set inTurn = false after a button is clicked(and prints out "clicked endturn").
\n
In my Dealor class, I have the update function which checks if player1's inTurn is false, if it is, I will print "player1 ended turn". However, after I clicked the button(printing out "clicked endturn"), the update function never prints out "player1 ended turn". I checked if the update function is running by printing player1's inTurn outside of if, and it turns out that it is always true and NEVER false, even after printing "clicked endturn". Any help on why is that the case? How would I fix it?
CODE BELOW:
public class Player : MonoBehaviour
{
public bool inTurn = true;
public void OnClick() {
Debug.Log("clicked endturn");
inTurn = false;
}
public bool isHisTurn() {
return inTurn;
}
}
public class Dealor : MonoBehaviour
{
private Player player1;
private new GameObject gameObject;
void Start() {
gameObject = new GameObject();
player1 = gameObject.AddComponent<Player>();
}
void Update()
{
//Debug.Log(player1.isHisTurn()); -> uncomments this will always print true, even after printing "clicked endturn" after clicking on button
if (!player1.isHisTurn()) {
Debug.Log("player1 ended turn");
}
}
}
screen-shot-2021-06-12-at-31033-am.png
(22.9 kB)
Comment