- Home /
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:
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.
Your answer
Follow this Question
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