Coroutine Scale goes past maxSize, but still keeps scaling.

Hey there guys, I’m having a bit of trouble with Coroutines, first the object wouldnt scale, but that issue has since been rectified, but now Im on the issue of the scaling not stopping.

I have provided a Video to better help toward a Correct Answer.

Youtube Video - Click Here

This is the C# Script I am currently using.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class growingScript : MonoBehaviour {

    public bool growing = true;
    public bool isGrown = false;
    public bool fertAdded = false;

    ParticleSystem fertParts;

    Transform dirt;

    public float growFactor = 0.1f;
    public float startGrow;
    public float fertMult;
    public float maxSize = 10.0f;

    void Start()
    {
        dirt = gameObject.transform.parent;
        fertAdded = dirt.GetComponent<plantingScript>().fertAdded;
        startGrow = growFactor;
        fertMult = startGrow * 2;
        StartCoroutine(Scale());
    }

    void Update()
    {
        fertAdded = dirt.GetComponent<plantingScript>().fertAdded;
    }

    public IEnumerator Scale()
    {
        while (growing)
        {
            while (maxSize > transform.localScale.x)
            {
                while (fertAdded)
                {
                    growFactor = fertMult;
                    transform.localScale += new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
                    yield return null;
                }
                while (!fertAdded)
                {
                    growFactor = startGrow;
                    transform.localScale += new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
                    yield return null;
                }
                yield return null;
            }
            while (maxSize < transform.localScale.x)
            {
                growing = false;
                isGrown = true;
                StopCoroutine(Scale());
                gameObject.layer = LayerMask.NameToLayer("Plant");
                yield return null;
            }
        }
    }

}

if(fertAdded)
{
growFactor = fertMult;
transform.localScale += new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
yield return null;
}
if(!fertAdded)
{
growFactor = startGrow;
transform.localScale += new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
yield return null;
}
yield return null;

Use if statements instead of whiles. You ended up with infinite cycle because it was not able to get out of the while cycle

Here, this works: https://0bin.net/paste/9jtkauYd+3AbC33F#F5cJjF2EkFFF+aEFVmiWMfFkN7b2Kod+iTJsAgi4JeH

Your mistake was when you tried to set growing to false inside the while that uses a true growing. When you set it to false it stops the while BEFORE it can stop the couroutine, so that’s why it isn’t working. Plus, if you want your script to run at yours grandmother’s PC then don’t use GetComponent~~() or Find() either inside Update(), LateUpdate() or FixedUpdate(). They are already updated at Start(). (I know this sounds weird, but it’s true. Just play a bit with that, and then you’ll find some approach that works).~~
Let me know if it works for you!