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 [i].position, emplacement.position) < 8f) && (tab [i].tag.ToString () == emplacement.tag.ToString ()) && ! dragging) {
switch (tab [i].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.
also, i'm calling the function verif_emplacements() in Update() ...
Answer by DoTA_KAMIKADzE · Nov 10, 2015 at 07:31 PM
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...