Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by leeroldy · Jan 01 at 01:03 AM · coroutineienumerator

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.

capture.png (13.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

186 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Add points every five seconds 1 Answer

Coroutine not working on android build 1 Answer

Unity - Lines after yield not getting executed 0 Answers

How do I make an enemy only damage once per second? 1 Answer

IEnumerator not working.WaitForSeconds never continiues. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges