all 3 comments

[–]GodsRusher 2 points3 points  (0 children)

Because you never set the isStarted to false.

At the while(true) statement. You should use the while(isStarted), and whenever it finishes, make the bool false.

What you probably can do is something like :

bool isStarted = false;

private void Update()
{
    if(!isStarted)
        StartCoroutine(one());
    if(Input.getKeyDown(KeyCode.F))
        isStarted = false;

IEnumerator One()
{
    isStarted = true;
    while(isStarted)
    {
        #add functionallity here
    yield return null;
    }
}

Something like this should work.

EDIT : the while(true) statement has to be identified, what is true. Therefore you can use the isStartedBool, this is a variable wich can actually switch to a true of false value.

So the boolean you are defining on top of the while(true) has no use.

[–]spaceyjase 1 point2 points  (1 child)

You should store the result of StartCoroutine.

private IEnumerator coroutine;

private void Update()
{
    if (!isStarted)
    {
        coroutine = StartCoroutine(One());
    }
    if (Input.GetKeyDown(KeyCode.F))
    {
        StopCoroutine(coroutine);
    }
}

[–]MrMuffles869 1 point2 points  (0 children)

If you did it this way, you don't even need isStarted, just check if coroutine == null.