Help with coroutine and IEnumerator on condition
I'm making a circle packing script and thus far I can randomly place a circle within the bounds of my canvas. I'll check for circle to circle hits later. This process is accomplished by placing a prefab that has a script attached to it to create a circle by adding lines to a LineRenderer. I then increase the radius until it hits the bounds of the canvas. I initially tried coding the instantiation in the Update() but then all the circles were present and growing together which is not what I want. I want to instantiate a prefab, grow it until it hits the canvas bounds, then instantiate another prefab. I read into coroutines and IEnumerator and it seems that is the route I need to take but I am not entirely sure and I have problems making it work. In my circle script for the prefab I have
IEnumerator Grow()
{
Vector3 pointPosition;
float angle = 0;
pointPosition = new Vector3(x + radius * Mathf.Cos(angle), y + radius * Mathf.Sin(angle), 0.0f);
lr.SetPosition(0, pointPosition);
for (int i = 1; i < res + 1; i++)
{
angle = (float)i / (float)(res) * 2.0f * Mathf.PI;
pointPosition = new Vector3(x + radius * Mathf.Cos(angle), y + radius * Mathf.Sin(angle), 0.0f);
lr.SetPosition(i, pointPosition);
outside = ((Mathf.Abs(x) + lnwidth + radius) > circlePacking.width / 2 || (Mathf.Abs(y) + lnwidth + radius) > circlePacking.height / 2);
if (outside) //true; if circle is outside stop growing
{
yield return outside;
}
else
{
radius += .00005f;
}
}
}
The circle instantiation is done with the following in the main script
IEnumerator GenerateCircles()
{
{
circlePosition = new Vector2(Random.Range(-width / 2, width / 2), Random.Range(-height / 2, height / 2));
circleObj = Instantiate(circlePrefab, circlePosition, Quaternion.identity);
circleObj.name = "Circle " + (count + 1);
circleObj.transform.SetParent(parentObj.transform, false);
circles.Add(circleObj);
while (circleObj.GetComponent<Circle>().outside == false)
{
yield return null;
}
}
}
[1]: /storage/temp/190609-capture.png
The circles are instantiated before one circle is finished growing. What do I need to change so that once a circle stops growing, another gets instantiated and grows and so on. Eventually I will be testing for circles in other circles and circles intersecting other circles. I need to get this instantiation/growing thing figured out first.