Enabling and Disabling movement

Hi everyone,
I am kind of new to this engine.
Right now I am just implementing some easy stuff, I just want to set a boolean to set whether my GameObject move or not.
What I did is like this,

 private void FixedUpdate()
    {
        if(isMovable)
            cubeMove();
    }

    void cubeMove()
    {
        transform.Translate(dropSpeed * Time.deltaTime * Vector3.back);
    }

It works, but I am curious about that is there anyway, that once I set something to false at runtime.
I can disable the whole if statement, so that it won’t check the if statement every loop?

You could move the functions that disable/enable GameObjects into their own private functions. This would allow you to only call them once when needed inside of another loop. (checking if the player pressed a key or something)

public bool CubeMovable;

private void FixedUpdate() {


}

private void CubeCanMove() {

    if (CubeMovable == false) {

        CubeMovable = true;

    }

}

private void CubeCantMove() {

    if (CubeMovable == true) {

        CubeMovable = false;

    }

}

Then only run it once.

private void FixedUpdate() {

    if (!onetime)
      {
        OneTimeFunction();
        onetime = true;
      }

}

Coroutines are perfect for this:

  private IEnumerator cubeMoveCoroutine()
     {
         while(true){
             cubeMove();
             yield return null;
         }
     }
 
     void cubeMove()
     {
         transform.Translate(dropSpeed * Time.deltaTime * Vector3.back);
     }

And then you can simply call…

StartCoroutine("cubeMoveCoroutine");

…to make the cube move, and…

StopCoroutine("cubeMoveCoroutine");

…to make it stop moving. That’s it, no constant checking necessary.

On a semi-related note, there’s no need to use Time.deltaTime in FixedUpdate, since that’s called at a fixed, frame independent rate anyway.