Execute code every .. sec

So i made a little script to execute specific code every … seconds… But it does not work:

void Update () {
	if (MouseVisible)
		Screen.showCursor = true;
	else
		Screen.showCursor = false;

	// List of functions
	HungerController ();
}

void HungerController ()
{
	float CurrentTime = 0;
	float NeededTime = 10;

	CurrentTime += Time.deltaTime;

	if (CurrentTime > NeededTime)
	{
		CurrentTime = 0;
		Hunger--;
	}
}

Can anyone help me?
Thanks in advance

You are executing the HungerController() function on everyframe. That resets everything in the function.
Basically move everything in update() except the float declarations. Put those in Start().

If you need to call HungerController() on its own from someplace else, then just move

float CurrentTime = 0;
float NeededTime = 10;

in Start() and CurrentTime += Time.deltaTime; in Update();

But i don’t see why not put them in update and have them in their own function.

Use a InvokeRepeating, way easier.

void Start(){
   InvokeRepeating("HungerController", 10f,10f);
}

void HungerController ()
{
   Hunger--;
}

or a coroutine (slightly more code):

void Start(){
    StartCoroutine(HungerController());
}

IEnumerator HungerController(){
   while(true){
      yield return new WaitForSeconds(10f);
      Hunger--;
   }
}

In both cases, no need for Update.

Easy answer, update is called every frame, that means HungerController is also called every frame and the values are reseted every time it is called.

Put the declarations outside HungerController. Like this:

    float CurrentTime = 0;
    float NeededTime = 10; // If you make it public you can tweak it in the inspector

    void Update () {
       if (MouseVisible)
           Screen.showCursor = true;
       else
           Screen.showCursor = false;
     
       // List of functions
       HungerController ();
    }
     
    void HungerController ()
    {
     
       CurrentTime += Time.deltaTime;
     
       if (CurrentTime > NeededTime)
       {
          CurrentTime = 0;
           Hunger--;
       }
    }

If you do not feel very confident in programming, I recommend you check this page if you haven’t done already: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn