How can I call a function once in Update according to a flag bool variable state false/true ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Waypoints : MonoBehaviour
{
    // Adding selectable behaviour at the end of the path,
    // as requested from the comments.
    public enum PathCompleteBehaviour
    {
        Stop,
        Loop,
        Reverse,
        Random,
        PingPong
    }
    [Header("Path Behaviour")]
    public PathCompleteBehaviour pathBehaviour;
 
    [Space(5)]
 
    [Header("Waypoints")]
    public GameObject[] waypoints;
 
    [Space(5)]
 
    [Header("Instances")]
    public GameObject[] instancesToMove;
 
    [Space(5)]
 
    [Header("Speed")]
    public float constantSpeed;
    public float minRandomSpeed, maxRandomSpeed;
    public bool randomSpeed = false;
    private bool randoms = true;
 
    public bool go = false;
 
    // Store parallel arrays for our movement parameters.
    private float[] movementSpeeds;    // Randomized speed per object.
    private int[] nextWaypointIndices; // Destination per object.
 
    private void Start()
    {
        // Changing terminology to be more standard.
        // - a "prefab" is a source asset you want to copy
        // - an "instance" is a copy of it that's been spawned in the scene
        instancesToMove = GameObject.FindGameObjectsWithTag("InstanceToMove");
 
        movementSpeeds = new float[instancesToMove.Length];
        nextWaypointIndices = new int[instancesToMove.Length];
 
        RandomSpeed();
    }
 
    private void Update()
    {
        if (randomSpeed == true)
            randoms = true;
        RandomSpeed();
 
        if (go)
            WaypointsAI();
    }
 
    private void WaypointsAI()
    {
        // We want to iterate over the instances, not over the waypoints.
        for (int i = 0; i < instancesToMove.Length; i++)
        {
            // Check to see if we reached the last waypoint.
            // If so, stop / loop / turn around as needed.
            int waypointIndex = nextWaypointIndices*;*

if (waypointIndex < 0 || waypointIndex >= waypoints.Length)
{
switch (pathBehaviour)
{
case PathCompleteBehaviour.Stop:
// Leave it where it is and skip to the next instance.
continue;
case PathCompleteBehaviour.Loop:
// Wrap front to back / back to front.
waypointIndex = (waypointIndex + waypoints.Length) % waypoints.Length;
break;
case PathCompleteBehaviour.Reverse:
// Bounce back to the point before last.
waypointIndex += (movementSpeeds > 0f) ? -2 : 2;
// Reverse direction.
movementSpeeds *= -1f;
break;
case PathCompleteBehaviour.PingPong:
// Between two last waypoints.
waypointIndex = (waypointIndex) % waypointIndex + 1;
break;
}
nextWaypointIndices = waypointIndex;
}

// Look up the waypoint we want to move toward next.
Vector3 waypoint = waypoints[waypointIndex].transform.position;

// Read this instance’s speed,
// to compute how far it should move in this frame.
float travel = Mathf.Abs(movementSpeeds_) * Time.deltaTime;_

// Apply the motion.
Vector3 position = instancesToMove*.transform.position;*
position = Vector3.MoveTowards(position, waypoint, travel);
instancesToMove*.transform.position = position;*

// If we’re within a configurable tolerance of the
// destination waypoint, set our destination
// to the next waypoint after it.
if ((position - waypoint).sqrMagnitude < 0.1f)
{
nextWaypointIndices += (movementSpeeds > 0f) ? 1 : -1;
}
}
}

private void RandomSpeed()
{
for (int i = 0; i < instancesToMove.Length; i++)
{
if (randomSpeed && randoms == true)
{
movementSpeeds = Random.Range(minRandomSpeed, maxRandomSpeed);
randoms = false;
}
else
{
movementSpeeds = constantSpeed;
}
}
}
}
The problem is with the RandomSpeed method in the Update.
I want to use the boolean flag randomSpeed to tell the user when he is in random speed mode or constant speed.
But in the Update I did:
if (randomSpeed == true)
randoms = true;
RandomSpeed();
I tried to use another boolean flag but it didn’t help. Still if randomSpeed is true also randoms is true.
I want that when I change randomSpeed to true keep in the inspector set to true but change the speed value/s in RandomSpeed only once.
Then if I will uncheck and change randomSpeed to false give all the movementSpeeds the constant speed value. And then if I change randomSpeed to true again give all the movementSpeeds random values but only once not every frame. Now it’s giving the movementSpeeds new random values every frame.
I don’t want to set randomSpeed to true and then to false. I want the user to know it’s true so it’s in random mode.
I want to change the state of the flag randomSpeed in real time while the game is running. So it must be in the Update I guess. Once I change the randomSpeed to true set random values but once in the method RandomSpeed if I will change the randomSpeed flag to false set in RandomSpeed method once to constant speed.
And again I don’t want to change the randomSpeed state inside RandomSpeed method. I want the user to understand and know in what state he is now: Random or Constant. So the randomSpeed variable bool should be stay in the current state he is but the effect inside RandomSpeed method should be once !
So when the game is running and the user is looking in the inspector he see the randomSpeed for example all the time checked but in the code the effect inside RanomSpeed will be once.

Okay, I didn’t read all the code, but if I get it right - to toggle things in code and call according function only once:

if (randomSpeed && !randomisedOnce) {
    randomisedOnce = true;
    RandomSpeed();
}
else if (!randomSpeed && randomisedOnce) { 
    randomisedOnce = false;
    RegularSpeed();
}

randomSpeed is public variable in inspector, randomisedOnce is private bool.
Both RandomSpeed() and RegularSpeed() will be called once when state is changed

In addition to Deadcow_'s Code.

You don’t have to double check a boolean.
Look at the code :

        bool condition = true;

        // Cleaner version
        if(condition) // if condition is true then CallFunc();
        {
            CallFunc();
            CallSecondFunc();
        }
        else // else condition  is false then call another func
        {
            CallAnotherFunc(); 
            condition = true;
        }

If you only want to do one thing in your if statement you dont even have to use these brackets :

    if (condition )
        CallFunc();
    else
        CallAnotherFunc();

Check Microsofts C# Reference for more information

Why not use a counter something like int?

int counter;
counter++;
if(counter == 1)
{
call something once
}

and reset counter upon calling the next function?