Calling a function once in the Update()

Hi everybody, i’m trying to call a simple function of increment :

void Circle_P_Bar() {
		current_amount += 8.3f;
}

i’m trying to call it in those conditions :

void verif_emplacements () {
	if (verif) {
		for (int i = 0; i < tab.Length; i++) {
			foreach (Transform emplacement in tab_emp) {
				if ((Vector3.Distance (tab _.position, emplacement.position) < 8f) && (tab *.tag.ToString () == emplacement.tag.ToString ()) && ! dragging) {*_

_ switch (tab .tag.ToString ()) {
* case “janvier”:_
tab_circles [0].SetActive (true);
_ janvier.collider.enabled = false;
janvier.renderer.material.mainTextureOffset = new Vector2 (0.5f, 0f);*_

Circle_P_Bar();

* break; …*
but the update function is calling the increment function every frame …
any help please, thanks.

If you need that code to be in Update then generally speaking you have 1 option - to use some sort of “trigger”, in your case boolean is enough. So you can do something like that:

  1. You already have “verif” variable, if it is not automatically set somewhere in code to true then just make it false inside your “once” function:

    void Circle_P_Bar()
    {
    current_amount += 8.3f;
    verif = false;
    }

  2. If the first variant is not suitable then make separate boolean - “AllowOnceFunction”.

Then make it true, and add this line to your “once” function:

void Circle_P_Bar()
{
    current_amount += 8.3f;
    AllowOnceFunction = false;
}

Now you can place it basically in many ways, depending on your expectations (e.g. what code should run only once):

void verif_emplacements()
{
    if (AllowOnceFunction && verif)
    {
        //...
    }
}

or

void verif_emplacements()
{
    if (verif)
    {
        //...
        if (AllowOnceFunction) Circle_P_Bar();
        //...
    }
}

or

void Update () 
{
    //...
    if (AllowOnceFunction) verif_emplacements();
    //...
}

or…