- Home /
Why does it seem like my boolean is true and false
I have a script that when a button is pressed it calls a function in another script setting a boolean to true. In that other script as long as im in that function the boolean is true but in anyone other function in the same script it is false (It starts out false). I can print both of them and one prints true and the other prints false any ideas?
function Update ()
{
// gameStart is false here
if(gameStart == true)
{
ballVX = gameObject.rigidbody2D.velocity.x;
ballVY = gameObject.rigidbody2D.velocity.y;
ballM = gameObject.rigidbody2D.mass;
if(ballVY <= .25 && ballVY >= -.25)
{
gameObject.rigidbody2D.AddForce(Vector2(0,1), -100);
}
gameObject.rigidbody2D.velocity = Vector2(ballVX, ballVY).normalized * ballSpeed;
}
}
function GameStart(isPlaying : boolean)
{
gameStart = isPlaying;
//gameStart is true here
}
Does the function GameStart gets called with isPlaying equals false when you let go of the button? If that happens before Update is called you will end up with gameStart equals false inside Update.
On a serious note you need to check all of the code that affects this variable. Look for the following
Where are all the places you are setting it to false
Are you setting the right instance of the variable. Do you have multiple copies of this script running around
Is it a public or static variable. Is there something else that is playing with it that you don't know about.
Your answer
![](https://koobas.hobune.stream/wayback/20220613152938im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Else and If 2 Answers
boolean questions get, set, in java. 1 Answer
Pause Button Help 2 Answers
animation not working 0 Answers