Countdown Management Out Of The Game

hi!

I have a problem with the time that manages energy: once I lose an energy, a countdown starts at the end of which I get an energy. However, if I close the game when the countdown is set a few seconds after the energy is restored and I return to the game, I rightly receive an energy but shortly afterwards it gives me another extra. It seems that the contdown since the return of the game has restarted since I closed the game. How can I do?
thanks!

public string saveLocation;
         private const float establishedTime = 120f;//1800f
         public  float timer;
            private float timePassed;
         private GameData gameData;
    
         private bool timerEnd = false;
         private bool openGame = true;
    
    
      public  float Timer
         {
             get
             {
                 return timer;
             }
    
             set
             {
                 timer = Mathf.Clamp(value, 0,value);
    
             }
         }
    
    
     private void Awake()
         {
             me = this;
             gameData = FindObjectOfType<GameData>();
             timerEnd = false;    
             saveLocation = "lastDate";
         }
    
    
     void Start () {
    
             if (gameData.return && gameDatai.lost && gameData howEnergy == gameData.maxEnergy - 1)
             {
                 SaveData();
             }
             if(  gameData.howEnergy < gameData.maxEnergy)//
             {
              
                 nEnergiesEarned = (int)(timePassed / establishedTime);
                 Add(nEnergiesEarned);
                 if (howEnergy < gameData.maxEnergy )
                 {
                     if (timePassed > establishedTime)
                         Timer = timePassed % establishedTime;
                     else Timer = ttimePassed - establishedTime;
                 }
                 openGame = true;
             }
          public float CheckDate()
         {
             currentDate = System.DateTime.Now;
             string tempSting = PlayerPrefs.GetString(saveLocation, "1");
    
             long tempLong = Convert.ToInt64(tempSting);
             DateTime oldDate = DateTime.FromBinary(tempLong);
             TimeSpan diff = dataCorrente.Subtract(oldDate);
             return (float)diff.TotalSeconds;
         }
       
         public void SaveData()
         {
             PlayerPrefs.SetString(saveLocation, System.DateTime.Now.ToBinary().ToString());
             PlayerPrefs.Save();
     ``}   
      public void Add(int energy)
         {       
             gameData.howEnergy += energy;
             PlayerPrefs.SetInt("enBase", gameData.howEnergy);
             PlayerPrefs.Save();
     }
    
     public void Restart()
         {
           
             if (openGame)
             {
                 StartCoroutine(Test());
               
                 return;
             }
             if ( Timer == 0)
             {
                 timeEnd = true;
             }
             if (tempoEnd && !openGame){
                 Timer = establishedTime;//1800f
                SaveData();
                 Add(1);
                 timeEnd = false;
                 }
         }
         IEnumerator Test()
         {
             yield return new WaitForSeconds(1f);
             openGame = false;
         }
         // Update is called once per frame
         void Update () { 
             if (gameData.extraEnergy == 0)
             {        
                 if ( gameData.howEnergy < gameData.maxEnergy)
                     Timer -= Time.deltaTime;           
             }
             Reset();
         }

Are you adding the script again at the next game, so you have two copies? Is there a different copy of the script on a different GameObject? You can figure it out by looking through the inspector, or else somewhere above putting in a Debug.Log(name) statement to see the name of the GameObject hosting this component.

hi! thanks for the reply, I checked and there is only in a game object, which is where I need it.

This isn’t necessarily related to your current symptom, but a couple of things I notice in your code:

timer = Mathf.Clamp(value, 0,value);
If value is greater than 0, this line accomplishes nothing. If value is less than 0, this causes undefined behavior: the documentation doesn’t say what happens if the value of “max” is LOWER than the value of “min”.

Basically, you should never pass the same variable as any two arguments of the Clamp function.

If you are trying to prevent the timer from ever going below zero, then you should say
timer = Mathf.Max(value, 0);

if ( Timer == 0)
Using == on a float is almost always a bad idea. It might be OK in this case because you are also assigning that value to the variable explicitly rather than calculating it using math, but generally you should use Mathf.Approximately or (if appropriate) use <= or >= instead.

In this case, you should probably get rid of the first thing entirely (just allow the timer to go below zero) and then check whether it’s <= 0 instead of == 0.

Also, your code looks like it was transcribed by hand and isn’t what you’re actually running. The last line of Update() calls a function called Reset(), but there’s no definition for Reset(); instead there’s a function called Restart(). On line 8 you define a variable called timerEnd, but on lines 89 and 95 you set a variable called timeEnd (with no “r”), and on line 91 you test a variable called tempoEnd.

I generally recommend you copy-paste the exact code that you’re running when asking for help. If you try to retype stuff or make up a simplified example (without actually testing the simplified example), there’s a risk that you’ll introduce new errors or change an important detail related to your problem without realizing it. This makes it hard to provide help.