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

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.