- Home /
Why does this function keep happening even when the bool assigned to make it happen is set to false?
So i have this code
void RepeatGround()
{
if (distance > 10f && generateGround == true)
{
generateGround = false;
Instantiate(ground, offset, Quaternion.identity);
}
offset = new Vector2(player.transform.position.x + 25f, -4.49f);
if (distance < 2f)
{
generateGround = true;
}
}
It should first check if the distance is bigger than 10 and if generateGround is set to true, then if it is it should set generateGround to false and instatiate the gameobject once, instead it keeps instatiating it each frame even if the bool has been set to false, which i already checked and it does get set to false. Any idea on what do i have wrong? Or on how to do what i want in other ways?
Thank you in advance
This all looks fine, so I'd have to assume the issue lies somewhere else in the script, like where generateGround is being defined, or maybe generateGround being set to true when it shouldn't be each frame. Are you able to paste the whole script?
⠀
My Other Recommendation would be add in a few Debug.Log(generateGround) before and inside the if statement, because according to what you're saying, they'll always return true since the if statement keeps returning true.
⠀
last thing to ask, is the script this is running on added to the object you instantiated, just to rule out any infinite recursion type thing.
public class GroundRepeater : MonoBehaviour
{
private Vector2 playerPos;
private Vector2 groundPos;
private Vector2 offset;
private float distance;
public GameObject player;
public GameObject ground;
private bool generateGround = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
distance = Vector2.Distance(playerPos, groundPos);
groundPos = ground.transform.position;
playerPos = player.transform.position;
RepeatGround();
Debug.Log(generateGround);
}
void RepeatGround()
{
if (distance > 10f && generateGround == true)
{
generateGround = false;
Instantiate(ground, offset, Quaternion.identity);
}
offset = new Vector2(player.transform.position.x + 25f, -4.49f);
if (distance < 2f)
{
generateGround = true;
}
}
}
it is attached to the gameobject it instatiates
Answer by AaronBacon · Aug 25, 2021 at 05:25 PM
Looking at the full script, I believe I can see the problem. Its largely just to do with the order things are declared.
⠀
If all you want to know is the solution, you need to move the "distance = Vector2..." line in the Update function to be 2 lines lower, underneath where you set groundPos but above RepeatGround()
⠀
As for why it happens, when you begin the game, playerPos and groundPos haven't been given any value, so are taking the default value of (0,0). This means that when you run:
⠀
void Update()
{
distance = Vector2.Distance(playerPos, groundPos);
groundPos = ground.transform.position;
playerPos = player.transform.position;
RepeatGround();
Debug.Log(generateGround);
}
⠀
The first time it runs, distance will return 0, (as you're setting distance first, before setting the other 2, and both groundPos and playerPos are still 0,0)
⠀
This means the first time RepeatGround() runs, distance is < 2f, so generateGround is set to true. Then the next frame, distance is set correctly this time, however the generateGround bool isn't set till after the Instantiate check, meaning that when it reaches the check for
⠀
"if (distance > 10f && generateGround == true)"
⠀
Both of these are true for the 2nd frame of the script running, so it instantiates an object. Normally everything is mostly set correctly from here on out, but the problem you're having is that this script instantiates another object with this script on. That new object has the same issue, since it begins with default values for playerPos and groundPos, so the cycle repeats, spawning a new object every other frame.
⠀
This means the solution to your issue is to move distance after playerPos and groundPos has been set, so that distance is calculated correctly the first time round:
⠀
void Update()
{
groundPos = ground.transform.position;
playerPos = player.transform.position;
distance = Vector2.Distance(playerPos, groundPos);
RepeatGround();
Debug.Log(generateGround);
}
⠀
Hope that helps, if there's anything you don't get feel free to comment.
This works perfectly, thank you very much and have a good day
Your answer
Follow this Question
Related Questions
Update function is not executing the bool 1 Answer
store instantiated in Array. c# 1 Answer
Problem when setting variable in an instantiated gameobject 0 Answers
Convert bool to char in C# 3 Answers
Why is my variable always true? 1 Answer