Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Capricornum · Feb 14, 2018 at 08:30 PM · instantiatecoroutineprofileriterate

Coroutine being iterated in an unrelated function

Dear Community,

Using the profiler I have found that a coroutine seems to be involved where it shouldn't. Since I am using coroutines for the first time I would like to understand why. I'll explain: In my game I have a grid of 8x8. Like so:

alt text

The grid (which is a 2 dimensional array of the custom class "Piece" is populated at runtime via a coroutine in my GridManager class like so:

 public class GridManager : MonoBehaviour {
     public Piece[] prefabs;
 
     private static int cols = 8;
     private static int rows = 8;
     private Piece[,] grid = new Piece[cols, rows];
     private List<Piece> subGrid = new List<Piece> ();
 
     void Start() {
         StartCoroutine (InitialiseGrid());
     }
 
     public IEnumerator InitialiseGrid () {
         for (int c = 0; c < cols; c++) {
             for (int r = 0; r < rows; r++) {
                 yield return new WaitForEndOfFrame ();
                 Vector2 gridPos = new Vector2 (c, r);
                 int randomIndex = Random.Range (0, prefabs.Length);
                 Piece newPiece = Instantiate (prefabs[randomIndex], gridPos, Quaternion.identity);
                 grid [c, r] = newPiece;
             }
         }
     }
 }

When the player clicks on one of the Pieces in the grid it and all its neighbors of the same color disappear. This is managed by my InputManager like so:

 public class InputManager : MonoBehaviour {
     public static bool yourTurn;
 
     public GridManager grid;
     public ParticleSystem explosion;
     public Ball[] balls;
 
     private int X;
     private int Y;
     private List<Piece> subGrid = new List<Piece>();
 
     void Update () {
         if (Input.GetMouseButtonDown (0) && yourTurn) {
             //calling a function that populates subGrid with the colored Pieces I clicked on;
             DestroyPieces ();
         }
     }
 
     void DestroyPieces () {
         int tempIndex = (int)subGrid [0].element;
         foreach (Piece p in subGrid) {
             //Instantiating ParticleSystem explosion with appropriate color;
             Ball ball = Instantiate (balls [tempIndex], p.transform.position, Quaternion.identity);
             StartCoroutine (ball.Ammo (heroes[tempIndex]));
         }
     }
 }

In the place of the Pieces little balls are spawned. These balls have on them a script called "Ball" which uses a coroutine to move them to a specific point like so:

 public class Ball : MonoBehaviour {    
     public float speed = 5f;
 
     public IEnumerator Ammo(Hero target)
     {
         Vector3 targetPos = target.transform.position;
         while (transform.position != targetPos) {
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards (transform.position, targetPos, step);
             yield return null;
         }
         // Do something with the target
         Destroy (this.gameObject);
     }
 }

When I check my profiler at the frame where I click somewhere in the grid, there is a massive spike in the CPU section. Now, I know the spike is due to my instantiation of the balls, and ParticleSystems and something with my additive material for the ParticleSystem called: Shader.EditorLoadVariant. But there is something going on where my "InitialiseGrid" Coroutine is involved. But that should have finished by now. Only the balls coroutine should be happening. Does anyone know why this is happening?

Thank you in advance.

alt text

game-screenshot.jpg (209.4 kB)
initialisegrid.jpg (214.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

98 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

Related Questions

Huge Framerate Drop 0 Answers

Delay Collecting Object by Player 0 Answers

Instantiate at Intervals 2 Answers

Profiler.BeginSample doesn't seem to work in CoRoutine? 0 Answers

How to decrease Gfx.WaitForPresent? (2D) 1 Answer


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