So, I found that if several balls impact a block on the same frame, they will all call the destroy function, which is bad as it decrements the block count for each impact, even if only one block is destroyed. I am aware I could FindObjectsOfType() but I am “concerned” with frame rate reduction. Is there a cleaner way to handle this issue?
Hi, welcome to the community!
With a game as simple as this one you shouldn’t be afraid of using the FindObjectsOfType method but I don’t see how that would solve your problem. What you could do is add a bool to your method to prevent it from being called more than once. Here’s an example, not sure how your code looks like, but the principle would be the same:
bool isBeingDestroyed;
private void OnCollisionEnter2D(Collision collision)
{
DestroyBlock(collision.gameObject);
}
private void DestroyBlock(GameObject block)
{
if(!isBeingDestroyed)
{
isBeingDestroyed = true;
//Do Whatever you want to do here.
}
}
Hope this helps!
Instead of using the counting process, I could just call it in update and see if any objects of the type exist. If none do, LoadNextScene().
But, yeah that’s a good solution you suggested. Thank you.
The magic of coding, there’s no real answer, you could actually use your solution in a process efficient way; child all the blocks to a game object and add the script, instead of looking every frame for all the blocks, you could count the child the object has, that way you could avoid using the FindObjectsOfTypemethod.
True enough. Another solution I had considered was creating a counter, and checking every 15th frame for all objects of type being present, if == 0 end level. But, I prefer this style of solution, as I feel the ability to know exactly what the program is doing, and having some ability to guess at the efficiency of said code is superior.
One of the “troubles” I am having with Unity is that so much of the action is obfuscated inside the engine.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.