Trying to call a function "end game()" from void Update. Ending up with triggering sound of the clip (because update function keeps on calling the endgame function i guess). Please Help.

void Update()
{
currentTime -= 1 * Time.deltaTime;
Timer.text = currentTime.ToString(“0”);

        if (currentTime <= 0)
        {
            currentTime = 0;
         //   Time.timeScale = 0;
            Endgame(); 
            

        }


void Endgame()
    {
        if (currentTime == 0)
        {

            Timer.gameObject.SetActive(false);
            gameOver.gameObject.SetActive(true);
            FindObjectOfType<AudioManager>().Play("GameOver");
            Debug.Log("GAME OVER");
        }
    }

Good day.

You should structure better your Update.

-First,

currentTime -= 1 * Time.deltaTime; 

is beein executed all frames (Yes, after that it becomes 0, but you are spending cpu doing this for nothing)

So maybe you should do somethinkg like:

if (currentTime >0)
{
    currentTime -= 1 * Time.deltaTime; 
}

-Second
In EndGame Method, you donty need to check for the current time value, it will be 0 always because this methid is called right after setting value to 0.

-Third

If you pretend the method EndGame to only be executed once, you need to change something to prevent beeing called all frames, now, as timer is 0 each frame this will be executed:

    if (currentTime <= 0)
     {
         currentTime = 0;
      //   Time.timeScale = 0;
         Endgame(); 
      }

So, i recommend you to create a new bool variable called for example “GameFinished” to know if you already runned the EndGame method.

So your code should be like this:

bool GameFinished = false;

void Update()
 {
     currentTime -= 1 * Time.deltaTime;
     Timer.text = currentTime.ToString("0");

     if (currentTime <= 0)
     {
         currentTime = 0;
      //   Time.timeScale = 0;
       if (GameFinished == false)
          {
             Endgame();              
            }
     }


   void Endgame()
 {
         GameFinished=true;
         Timer.gameObject.SetActive(false);
         gameOver.gameObject.SetActive(true);
         FindObjectOfType<AudioManager>().Play("GameOver");
         Debug.Log("GAME OVER");
     
 }

@RoyBoy

I like explaining everything so that way others know what is going on. So hopefully this explained well enough.

bool gameFinished = false;
void Update() {
         // Subtract from our timer accordingly
         //Only If our timer is greater than 0
          if (currentTime >0)
          {
              currentTime -= 1 * Time.deltaTime;
          }

         //Make sure our UI shows our timer
         Timer.text = currentTime.ToString("0");

         // If our timer is less than or equal to 0
         // and the game is not finished
         if (currentTime <= 0 && gameFinished == false)
         {
             //Start the EndGame() function
             Endgame(); 
         }
 }
 
 void Endgame() {
             //Set our timer to 0
             currentTime = 0;

             //Turn off the timer display
             Timer.gameObject.SetActive(false);

             //Turn on the game over object
             gameOver.gameObject.SetActive(true);

             //Play our game over sound
             FindObjectOfType<AudioManager>().Play("GameOver");

             //Debug message to make sure it all works
             Debug.Log("GAME OVER");

             //Set our game as finished so 
             //this doesn’t run a second time
             gameFinished = true;
}

Thanks Everyone, specially @KittenSnipes @tormentoarmagedoom
But before I tried your codes, I changed my code a little bit and its working perfect now.

bool isEndgame = false;

void Update()
    {
        if (isEndgame == false)
        {
            ClockTimer();
        }
                    
    }

void ClockTimer()
    {
        currentTime -= 1 * Time.deltaTime;
        Timer.text = currentTime.ToString("0");

        if (currentTime <= 0)
        {
            currentTime = 0;
            isEndgame = true;
            EndGame();
        }

    }

void EndGame()
    {
        Timer.gameObject.SetActive(false);
        gameOver.gameObject.SetActive(true);
        FindObjectOfType<AudioManager>().Play("GameOver");
            
            Debug.Log("GAME OVER");
       
    }