- Home /
[Help] Calling a function, involves another function that is completely unrelated
Dear Community,
I am very confused because in my little project I am calling a function that should not be connected in any way to another (completely unrelated) coroutine. However when I check with the Profiler, apparently that coroutine seems to be involved in the process.
Its like this: On my GridManager Script I am starting a coroutine InitialiseGrid in Start(). So a grid of 8x8 colored pieces is initialized. Then, when I click on one of the colored pieces I am calling a function in my InputManager, that creates a List of equally colored pieces, iterates through them and in their place spawns a little ball with a coroutine on it to move it.
Like this:
public class GridManager : Monobehavior {
public Piece piece; //this is a prefab
private Pieces[,] grid = new Pieces[8, 8];
private List<Piece> subGrid = new List<Piece>();
void Start {
StartCoroutine(InitialiseGrid());
}
IEnumerator InitialiseGrid {
for (int c = 0; c < 8; c++) {
for (int r = 0; r < 8; r++) {
Vector2 newPos = new Vector2 (c, r);
Piece p = Instantiate (piece, newPos, Quaternion.identity);
grid [c, r] = p;
yield return null;
}
}
}
public List<Piece> CollectPieces (Vector2 clickedPos) {
int x = (int)clickedPos.x;
int y = (int)clickedPos.y;
subGrid.Add (grid[x, y]);
if (x+1 < 8) {
if (grid [x+1, y].color == grid[x, y].color) {
subGrid.Add (grid[x+1, y]);
}
}
return subGrid;
}
}
public class InputManager : Monobehavior {
public Ball ball; //this is a prefab
public GridManager gridManager;
private List<Piece> subGrid = new List<Piece>();
void Update ()
{
if (Input.MouseButtonDown(0)) {
//determine Position where clicked. If clickedPos is within grid:
subGrid = gridManager.CollectPieces(clickedPosition);
DestroyPieces ();
}
}
void DestroyPieces () {
foreach (Piece p in subGrid) {
Ball b = Instantiate (ball, p.transform.position, Quaternion.identity);
StartCoroutine (b.Ammo());
Destroy (p.gameObject);
}
}
}
Now when I check with my profiler at the moment where I click the mouse and some pieces get destroyed and some balls go flying, it says:
So apparently my InitialiseGrid Coroutine seems to be involved when I click. But it shouldn't. Does anybody know what is happening here? I am afraid I might have done something completely wrong. Thank you in advance!!
Please, does anyone have any idea?
I just realized, that moving the mouse over the screen results in the EventSystem.Update() to do something related to Input$$anonymous$$anger.InitialiseGrid(); Again I see this in the profiler.
Your answer
Follow this Question
Related Questions
Update scene every 5 minutes 2 Answers
How to make a spawner wait for x seconds? 2 Answers
How Stop Update function of other scene 1 Answer
Figuring out Coroutines 3 Answers
Profiler.BeginSample doesn't seem to work in CoRoutine? 0 Answers