I am in the process of creating a side-scrolling shoot’em up game with a pooling system of background elements that move from right to left to create a parallax effect.
The background elements are placed correctly one beside the others but if I reset the game using “SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);” the pooling system still works but there are gaps between the background elements.
Here is the code I used to reset the game:
using UnityEngine;
using UnityEngine.SceneManagement;
public class resetScript : MonoBehaviour
{
private void Update()
{
// Check if the "R" key is pressed
if (Input.GetKeyDown(KeyCode.R))
{
// Call a method to reset the game
ResetGameFunction();
}
}
private void ResetGameFunction()
{
// Reload the current scene
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
I tried LateUpdate but the problem remained the same.
Any help would be appreciated and many thanks in advance for your feedback.
Found the solution on my own but it is rather a “Patch” than a proper solution as I think I would need to reinitialize every positions and states of the parallax objects to be sure that when I reset by reloading the scene I have exactly all the same values like when I start the game at the beginning.
What I have done is that if I pause the game by freezing time and then reset the game with “SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);” it seems to work that all positions and states are like when I start the game.
Yeah like @Kurt-Dekker said, background items are not really a good candidate for pooling, as generally you can have only few elements visible at the same time and they get out of view slowly, which means the time between creating new items and destroying old ones is big enough (hundreds to thousands of frames) to not cause any noticeable GC issues.
Thanks both of you ( @Kurt-Dekker and @Nad_B ) for your replies but, to be honest, I’m unsure to have understood them properly as a beginner, sorry.
With what I seem to understand is that my choice of using a pooling system to move background elements from right to left to create a parallax effect may be the wrong approach in terms of complexity, bugs and edge cases.
Is it also maybe too memory consuming?
As for now I found it to be very efficient in what I try to achieve as I have BG elements that are cloned to fill the screen width and once they go out of sight they are replaced to the far right of the screen which works with a non moving Camera and a moving Camera. I can change the pictures using an animator for variety or adaptation to the environment.
The problem I had when I reset the game scene is that it created gaps between the BG elements and I realized that the pooling system, as Kurt-Dekker correctly pointed out, seems indeed unreliable as I have no 100% real control of it, it seems respectively I couldn’t figure out why it’s doing the gaps after I reset the scene. When I reset the scene after I pause the game by freezing time, the problem with gaps disappeared.
That said (written) would there be a better system/approach to make a similar system as the one described not using a pooling system?
Often you’ll find that simply doing #1 results in #2 already happening.
If you don’t NEED pooling, don’t DO pooling. It’s that easy.
Focus on the problem you have. Don’t reach for pooling if you don’t have a problem that absolutely CAN be solved by pooling. Pooling-solvable problems are surprisingly rare. Unless you’re making and destroying dozens / hundreds of things PER FRAME EVERY FRAME, you will probably not see a benefit from pooling. In those cases pooling is just pointless complexity that can and WILL BREAK if you misuse it.