StackOverflow Problem when consistently checking a gameObject

Hey before I start I want to let everyone know I’m not only new to this forum but Unity and C# itself so I apologize if there is an easy solution or other silly mistakes.

Alright so basically what I am trying to do is switch the gravity of my player when they hit space, to achieve this I am checking the players transform.position.y and seeing if it is at it’s designated height and if it’s not I add force.

The area of code:

private void ChangeGravity()
    {
            if (rb.position.y >= 10f)
            {
                SAF = false;
                rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;  
            }
            else
            {
                rb.AddForce(0, VerticalForce * Time.deltaTime, 0);
                ChangeGravity();
            }
    }

For clarification SAF is a precautionary measure so that the player can’t spam the space button.
Also VerticalForce = 2f and through my testing I have determined that it is possible for the if statement to be true (this test was by setting the y to 10)

Now here’s the error:

StackOverflowException
UnityEngine.Rigidbody.AddForce (Vector3 force, ForceMode mode)
UnityEngine.Rigidbody.AddForce (Single x, Single y, Single z) (at C:/buildslave/unity/build/Runtime/Dynamics/ScriptBindings/Dynamics.bindings.cs:171)
PlayerMovement.ChangeGravity () (at Assets/Scripts/PlayerMovement.cs:21)
PlayerMovement.ChangeGravity () (at Assets/Scripts/PlayerMovement.cs:22)
(The final line repeats a bunch but I cut that out)

The Entire Script: Click Me

,Hey before I start I want to acknowledge the fact that I am new to Unity and C# so while there may be an easy fix I do not see it.

After a few hours of troubleshooting I have decided to bring my Stack overflow problem here, basically what I’m trying to do is check a game objects height and if it is not at it’s desired height I want to add to it’s height to get there. I’m doing this in a most likely ineffective way but here’s what I have:

private bool SAF = false;

    private void ChangeGravity()
    {
        if (SAF == false)
        {
            rb.AddForce(0, VerticalForce * Time.deltaTime, 0);
            
            if (rb.position.y >= 10)
            {
                SAF = true;
                
            }
            
            ChangeGravity();
        }
        if (SAF == true)
        {
            rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
            SAF = false;
        }

By the way VerticleForce is set to 2f right now but later this number will most likely change. Also something to keep in mine is in my testing if the game object I’m referencing is at 10f (meaning i set it to that) the code does activate so that’s not it.
Now here’s the error:

StackOverflowException
UnityEngine.Rigidbody.AddForce (Vector3 force, ForceMode mode)
UnityEngine.Rigidbody.AddForce (Single x, Single y, Single z, ForceMode mode) (at C:/buildslave/unity/build/Runtime/Dynamics/ScriptBindings/Dynamics.bindings.cs:166)
PlayerMovement.ChangeGravity () (at Assets/Scripts/PlayerMovement.cs:16)
PlayerMovement.ChangeGravity () (at Assets/Scripts/PlayerMovement.cs:24)
(that last line repeats for awhile) `

Link to the whole script here:

While it may seem logical to use a recursive algorithm to reach a target height, the problem is that the physics engine must have an opportunity to calculate the force being applied and then move the object. That can’t happen until the next FixedUpdate. The object will not move within the current fixed update, which means there’s no way this implementation will do anything other than recurse until the stack overflows.


Stop recursing and allow the next FixedUpdate to subsequently measure and consider more force (along with associated minor complications and tuning in doing so).