- Home /
Coroutine Local Scale Stops Milliseconds After Starting
Hey there guys, I'm having an issue with a growing (Scaling) Coroutine that stops very very shortly after starting, I have a plant that starts at the size of 0.1 that grows to a size of 1.0, but as soon as the Coroutine starts, it seems to stop near instantly, the plant scales by 0.002, making the plant size 0.102 but then stops there, while i am still receiving the debug logs of...
'Growing True'
'Max Size is Higher...'
'Fertilizer not Added'
ect...
But the plant fails to scale.
Below is the code I currently have that is attached to an instantiated plant from a seed.
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;
float startGrow;
float fertMult;
public float maxSize = 10;
void Start()
{
dirt = gameObject.transform.parent;
fertAdded = dirt.GetComponent<plantingScript>().fertAdded;
startGrow = growFactor;
fertMult = startGrow * 2;
StartCoroutine(Scale());
}
void Update()
{
growing = true;
fertAdded = dirt.GetComponent<plantingScript>().fertAdded;
}
public IEnumerator Scale()
{
Debug.Log("Coroutine started");
while (growing)
{
Debug.Log("Growing True");
while (maxSize >= transform.localScale.x)
{
Debug.Log("Max Size Higher than Current Size, Growing");
gameObject.transform.localScale += new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
while (fertAdded)
{
Debug.Log("Fertilizer Added");
growFactor = fertMult;
yield return null;
}
while (!fertAdded)
{
Debug.Log("Fertilizer not Added");
growFactor = startGrow;
yield return null;
}
yield return null;
}
while (maxSize <= transform.localScale.x)
{
Debug.Log("Max Size Lower than Current Size, Stop Growing");
growing = false;
isGrown = true;
StopCoroutine(Scale());
gameObject.layer = LayerMask.NameToLayer("Plant");
yield return null;
}
}
}
}
Answer by FuryFight3r · Jun 09, 2017 at 09:02 PM
Issue Fixed!
public IEnumerator Scale()
{
Debug.Log("Coroutine started");
while (growing)
{
Debug.Log("Growing True");
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;
}
}
}
Simply by moving the "transform.localScale" into the fertAdded while and !fertAdded while.
The issue I'm having now is that the plant keeps scaling and doesnt stop once it IS or is OVER the maxSize, but that will have to be asked in another question to avoid getting any naughty strikes on my account.