title: unity c# for ... while ..the code crushes ,counting time upon user keypress and user key up
body: <p>i am doing a key press and count time 1 second to 3 seconds and the object will increase its speed upon how long you press the key and as for key up i am doing when user have let up the key , the object will drop its top speed to zero, not instantly , slowly.</p>
  <pre><code>public class PlayerMoment : MonoBehaviour
  
  {
  private float _maxTime = 3f;
  private float i = 0f;
  private float _jumpForce = 5.0f;
  private float _right = 1f;
  private float _left = -1f;
  private float _time = 0f;
  private float _endTime = 1f;
  private Rigidbody2D _ridgi;
  private bool _released = false;
  // Start is called before the first frame update
  void Start()
  {
      _ridgi = GetComponent&lt;Rigidbody2D&gt;();
      
  }
  
  void Update()
  {
      StartCoroutine(&quot;TimeInCrease&quot;);
  }
  
  
  IEnumerator TimeInCrease()
  {
  </code></pre>
  <p>get the input axis</p>
  <pre><code>    float move = Input.GetAxis(&quot;Horizontal&quot;);
      yield return new WaitForSeconds(1f);
      _time += 1f;
  </code></pre>
  <p>while _time bigger than one second and if key press a and if axis is less than -0.1
  _left will decrease 10f continuesly</p>
  <pre><code> while(_time &gt;= 1f)
  {
   if (Input.GetKey(&quot;a&quot;))
   {
    if (move &lt;= -0.1f)
   {
  _left -= 10f;
    }
    }
  </code></pre>
  <p>if user key up at 3seconds or 2 seconds or 1 second
  will start counting time in decrease
  if time equals zero will stopcoroutine to stop time decrease</p>
  <pre><code>        if(Input.GetKeyup(&quot;a&quot;) &amp;&amp; _time &gt;= 3f || _time &gt;= 2f || _time &gt;= 1f)
          {
              StartCoroutine(&quot;TimeDecrease&quot;);
  
              if(_time == 0f)
              {
                  StopCoroutine(&quot;TimeDecrease&quot;);
                  break;
              }
             
          }
          _ridgi.velocity = new Vector2(_left * Time.deltaTime, _ridgi.velocity.y);
      }
      
     
     
     
      
  
  
    }
  
  
   IEnumerator TimeDecrease()
  {
      Debug.Log(&quot;timeDecrease fires&quot;);
      yield return new WaitForSeconds(0.3f);
      _time -= 1f;
  </code></pre>
  <p>every 0.3 second will decrease_time value
  if _time reached bigger than 3second or 2 second or 1 second
  will stop to increase time</p>
  <pre><code> if(_time &gt;= 3f || _time &gt;= 2f || _time &gt;= 1f)
      {
          StopCoroutine(&quot;TimeInCrease&quot;);
      }        
      
      
  }
  }
  </code></pre>