- Home /
Sometimes the bool value is not changing
So I have this mini game, where I spawn two identical prefabs, make them children of GameObject called "WorkerSpawner" and then move that game object with both children to center, when it reaches the center, the line above them starts extending and when a mouse button is pressed or it reaches x axis limit it stops extending and drops down. with a gravity scale set to 3.
My problem is that when I check if the "WorkerSpawner" has arrived to the center, the "HaveWorkersArrived()" returns bool values correctly all the time, but "TouchInputOn" does not, it sometimes returns true, but most of the time it returns only false. Why ?
Spawner class:
public bool HaveWorkersArrived()
{
if (transform.position != centerPosition)
{
liScript.TouchInputOn = false;
return false;
}
else
{
liScript.TouchInputOn = true;
return true;
}
}
Line class:
public GameObject line;
GameObject workerz;
private SpawnWorkers workerSpawner;
public bool TouchInputOn;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
workerz = GameObject.FindGameObjectWithTag("WorkerSpawner");
workerSpawner = workerz.GetComponent<SpawnWorkers>();
}
private void Update()
{
if (TouchInputOn == true && workerSpawner.HaveWorkersArrived() == true)
{
line.transform.localScale = new Vector3(line.transform.localScale.x + 0.2f * speed, line.transform.localScale.y, 0);
if (Input.GetButtonDown("Fire1") || line.transform.localScale.x >= 1f)
{
rb.gravityScale = 3f;
TouchInputOn = false;
}
}
}
Answer by Compii · Jul 19, 2020 at 10:50 AM
So the way that the if function works in C# is that when you use if with && like you did,
if (TouchInputOn == true && workerSpawner.HaveWorkersArrived() == true)
it will first check if TouchInputOn is true. If it isnt, it wont even check HAveWorkersArrived(). However, TouchInputOn only gets set to true after executing HaveWorkersArrived(). So you could fix this by just changing the order of the if statement:
if (workerSpawner.HaveWorkersArrived() == true && TouchInputOn == true)
So now I want to give you some more tips how you could improve your code. When you use bools in if statements you can just write the variable without == true because it does the same:
if (workerSpawner.HaveWorkersArrived() && TouchInputOn)
That does the same as the Code above. If you check for false, you can just use "!" which basically inverts the bool:
if (!workerSpawner.HaveWorkersArrived() && !TouchInputOn)
Furthermore you check if touchInputOn and HaveWOrkersArrived() are true, but since you set TouchInputOn in HaveWorkersArrived() to the same value as it returns you dont need to check TouchInputOn too because it'll always be the same. You can just do:
if (workerSpawner.HaveWorkersArrived())
Hope everything now works for you!
I tried changing the order as well, it still doesn't work. Also if I only leave "if(workerSpawner.HaveWorkersArrived())" it doesn't stop line from extending when it drops down.
Your answer
Follow this Question
Related Questions
How Do I check if a Bool was True a few moments ago 2 Answers
How to change a Bool for only a single prefab GameObject? 1 Answer
Why is this bool auto turning off? 0 Answers
my button dosent work, became a light switch instead of a normal button (like a bell) 1 Answer
Boolean and input relation 0 Answers