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.
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;
}
}
}
}