Coroutine doesnt turn bool value?

I believe agents should not go to the destination that was at because it makes timeToAttack false in Countdown. this is the code. After counter2=0 they spawn at the positions but than they follow the destination again.

void Update(){
   if (timeToAttack==true)
         {
        
             if (enemy1select)
             {
                 agent1.SetDestination(enemyPlayer1.transform.position);
                 FaceTarget1();
               /*  if (combat1.isDeath==true)
                 {
                     Debug.Log("Calıstı 1");
                     enemyPlayer1 = null;
                     Debug.Log("Calıstı 2");
                     enemyPlayer1 = GameObject.FindGameObjectWithTag("Player");
                     Debug.Log("Calıstı 3");
                     agent1.SetDestination(enemyPlayer1.transform.position);
                     Debug.Log("Calıstı 4");
                     FaceTarget1();
                 }*/
             }
             if (enemy2select)
             {
                 agent2.SetDestination(enemyPlayer2.transform.position);
                 FaceTarget2();
             }
             if (enemy3select)
             {
                 agent3.SetDestination(enemyPlayer3.transform.position);
                 FaceTarget3();
             }
          
         }
       
   
         if (enemyPlayer1 != null && agent1G != null)
         {
             float distance1 = Vector3.Distance(enemyPlayer1.transform.position, agent1G.transform.position);
             if (distance1 <= 7)
             {
                 animator1.SetBool("Attack", true);
             animator1.speed = 1.5f;
             }
         }
       
       
         
      
         if (enemyPlayer2 != null && agent2G != null)
         {
             float distance2 = Vector3.Distance(enemyPlayer2.transform.position, agent2G.transform.position);
             if (distance2 <= 7)
             {
                 animator2.SetBool("Attack", true);
                 animator2.speed = 1.5f;
             }
         }
      
         if (enemyPlayer3 != null && agent3G != null)
         {
             float distance3 = Vector3.Distance(enemyPlayer3.transform.position, agent3G.transform.position);
             if (distance3 <= 7)
             {
              
                 animator3.SetBool("Attack", true);
                 animator3.speed = 1.5f;
             }
         }
       
     }
  IEnumerator Countdown(int seconds)
     {
         canClick = true;
         waveTimer = GameObject.Find("Canvas/Timer").GetComponent<Text>();
         int counter = seconds;
         int counter2 = seconds;
         while (counter > 0)
         {
            yield return new WaitForSeconds(1);
             counter--;
          
             waveTimer.text = "" + counter;
         }
         if (counter == 0)
         {
             timeToAttack = true;
             canClick = false;
             counter = seconds;
         }
         while (counter2 > 0)
         {
             yield return new WaitForSeconds(1);
             counter2--;
             waveTimer.text = "" + counter2;
             if (counter2 == 0)
             {
                 timeToAttack = false;
                 canClick = true;
                 Debug.Log("Fight Over!");
                 if (agent1G != null)
                 {
                     agent1G.transform.position = pos1;
                     agent1G.transform.rotation = rot1;
                 }
                 if (agent2G != null)
                 {
                     agent2G.transform.position = pos2;
                     agent2G.transform.rotation = rot2;
                 }
                 if (agent3G != null)
                 {
                     agent3G.transform.position = pos3;
                     agent3G.transform.rotation = rot3;
                 }
                 agent1 = null; agent1G = null;
                 agent2 = null; agent2G = null;
                 agent3 = null; agent3G = null;
                 enemyPlayer1 = null;
                 enemyPlayer2 = null;
                 enemyPlayer3 = null;
                 counter = seconds;
             }
         }
     
     }

You named your method “void()”? That would not be my first choice! Your problem description is not clear, but I might suggest using Debug.Log to follow your code logic Tips for new Unity users

ahaha no its in update , i copied only he part wich has problem ,

In IEnumerator Countdown there s counter and counter2, when counter=0 it makes timeToAttack=true but after that I said make it false when counter2=0, so my other part of scrip is in update method but when counter2=0 agents still moves to targets

Please show the Debug.Log output and the variable values at runtime

5334477--537966--Adsız.png

  IEnumerator Countdown(int seconds)
    {
        canClick = true;
        waveTimer = GameObject.Find("Canvas/Timer").GetComponent<Text>();
        int counter = seconds;
        int counter2 = seconds+5;
        while (counter > 0)
        {
           yield return new WaitForSeconds(1);
            counter--;
          
            waveTimer.text = "" + counter;

        }
        if (counter == 0)
        {
            timeToAttack = true;
            canClick = false;
            counter = seconds;
        }
        while (counter2 > 0)
        {
            yield return new WaitForSeconds(1);
            counter2--;
            waveTimer.text = "" + counter2;

            if (counter2 == 0)
            {

                timeToAttack = false;
                Debug.Log("timeToAttack= "+timeToAttack);
                canClick = true;


                Debug.Log("Fight Over!");
                if (agent1G != null)
                {
                    agent1G.transform.position = pos1;
                    agent1G.transform.rotation = rot1;
                }
                if (agent2G != null)
                {
                    agent2G.transform.position = pos2;
                    agent2G.transform.rotation = rot2;
                }
                if (agent3G != null)
                {
                    agent3G.transform.position = pos3;
                    agent3G.transform.rotation = rot3;
                }
                agent1 = null; agent1G = null;
                agent2 = null; agent2G = null;
                agent3 = null; agent3G = null;

                enemyPlayer1 = null;
                enemyPlayer2 = null;
                enemyPlayer3 = null;
               
            }
        }
     
    }

and in update method

     if (timeToAttack==true)
        {
        

            if (enemy1select)
            {
                agent1.SetDestination(enemyPlayer1.transform.position);
                FaceTarget1();
           
            }
            if (enemy2select)
            {
                agent2.SetDestination(enemyPlayer2.transform.position);
               
                FaceTarget2();
            }

            if (enemy3select)
            {
                agent3.SetDestination(enemyPlayer3.transform.position);
                FaceTarget3();
            }
          
        }
        if (!timeToAttack)
        {
            Debug.Log("It is false");
        }

Ok, that’s a start. Are those the values you expect? If not, why not?

Well it debugs when it makes timeToAttack=false in IEnumerator but I said if(false) then debug it again. But it did not do it in uptade method so somehow this chagne in the variable cant be used in update. It doesn’t make sense to me. Wondering what im missing here

would you like to see the sholw script?

What value isn’t correct? Find it, then place your Debug.Log statements around where ever it changes and find out why. Put Debug statements before and after every IF statement, and at the beginning of each routine and print out ALL the variable values.

I figured it out. I’m using photon plugin so i start this coroutine with that. But still, my value changes but in the update method once agents spawn at positions they run back to target even that lines are under if(timeToAttack=true)

As you can see I wrote all agents and enemies =null but they still can move to eachother