Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problem in Update Function

Discussion in 'Scripting' started by akkayaozi44, Apr 29, 2020.

  1. akkayaozi44

    akkayaozi44

    Joined:
    Apr 18, 2020
    Posts:
    11
    I have a update function like this.

    Code (CSharp):
    1.  
    2.  void Update()
    3.     {
    4.  
    5.         if(TurnControl==false)
    6.         {
    7.             camera_zoom();
    8.    
    9.         }
    10.         else if(TurnControl==true)
    11.         {
    12.             camera_normal();
    13.             StartCoroutine(Wait());
    14.         }
    15.     }
    16.  
    17.     IEnumerator Wait()
    18.     {
    19.         yield return new WaitForSeconds(5);
    20.         TurnControl = false;
    21.           }
    22.  
    23.  
    If turn control is false camera_zoom function calling. In camera_zoom function turncontrol set to true.

    The problem is sometimes turncontrol can't set to true. Sometimes calling camera_zoom function 4-5 times.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    You are starting the wait Coroutine every frame if the turn control is true. That means that you have a ton of that coroutine all starting, and all setting TurnControl to false 3 seconds later.
     
  3. akkayaozi44

    akkayaozi44

    Joined:
    Apr 18, 2020
    Posts:
    11
    what can i do ? do you have any suggestion
     
  4. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
    Start it once, not 60 times per second.
     
  5. akkayaozi44

    akkayaozi44

    Joined:
    Apr 18, 2020
    Posts:
    11
    how can i do that ?
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Probably something like this

    Code (CSharp):
    1.  
    2. private bool waiting = false;
    3.  
    4.  void Update()
    5.     {
    6.  
    7.         if(TurnControl==false)
    8.         {
    9.             camera_zoom();
    10.  
    11.         }
    12.         else if(TurnControl==true)
    13.         {
    14.             camera_normal();
    15.             if (waiting == false)
    16.             {
    17.                 StartCoroutine(Wait());
    18.             }
    19.         }
    20.     }
    21.  
    22.     IEnumerator Wait()
    23.     {
    24.         waiting = true;
    25.         yield return new WaitForSeconds(5);
    26.         TurnControl = false;
    27.      }
    28.  
    29.  
     
  7. akkayaozi44

    akkayaozi44

    Joined:
    Apr 18, 2020
    Posts:
    11
    Thanks for help. But i want if TurnControl equal to true, the StartCoroutine to be called .
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    That's what it does, but just calls it one time instead of hundreds of times. Though I would change your "if/else if" since it is a bit nonsensical.