- Home /
Best way to deal with DontDestroyOnLoad when returning back to the same scene?
I have a background that is the same throughout my game. It starts in the start screen scene, where I apply DontDestroyOnLoad to it. However, what happens if the player returns to the start screen? I don't want a duplicate background object to appear! There are obvious ways to make it so that the object doesn't appear when returning, such as by setting a global flag variable, but that seems a bit messy. Is there a simpler way?
Answer by raxashafique · Nov 23, 2021 at 10:11 PM
One quick solution would be to make your BG Singleton and handle spawning via Singleton Logic >> As Singletons can only have one object of said type at any given time.
Although I am not a fan of singletons, but here might just be the best example to use singleton.
Thank you for the suggestion! I think I am going to go with creating an entirely new scene BEFORE the main menu that never gets returned to. That way I can set everything up in that initial scene then immediately go to the real first scene. This will allow for creating the DontDestroyOnLoad objects as well as loading from the save file.
Your answer
Follow this Question
Related Questions
When die - reset score from the last scene 2 Answers
DontDestroyOnLoad duplicate prevention code is deleting the original player 2 Answers
How to get rid of the duplicates created by DontDestroyOnLoad? 3 Answers
keep static/global object through scenes 1 Answer
Switching Scenes Via Button Only Works for One Transition 1 Answer