- Home /
After "Restarting/Reloading" a level, I have multiple pause screens.
Hello all! New to Unity here.
I have successfully implemented a restart button on my game's UI, however when I utilize my pause screen, it seems I have to click through multiple canvasses before I can finally un-pause. The number of clicks/canvasses correlate to the # of times I reloaded the screen. Any advice or help is appreciated! Thank you!
public void RestartLevel() { SceneManager.LoadScene("level1"); }
I have a game manager script utilizing the DontDestroyOnLoad(gameObject); Could that be the problem?
Yeah, that's probably part of the problem, but we would really need to see the part of code responsible for showing the pause screen
Answer by EDevJogos · May 19, 2021 at 11:53 PM
@cat_dawg I think what you said is problably the reason for you to having multiple instances of the same object, since when you load a scene it will instatiate all the objects belonging to it and bring over all objects that are tagged dont destroy on load from the current scene.
The most likely solution to your problem is to check if a instance of that object already exists and if so destroy it.
You can do that with the following lines of code:
private static YourClassName Instance;
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
}
I had a feeling, I just didn't know the right words! I will try this and hopefully it fixes my issue. Should I be putting this in my "Pause" script? Could I also utilize this line of code but still keep the DontDestroy script on my Game Manager?
Thank you so much!!
Use these code on every object that has dontdestroy, it will prevent the game from creating duplicates of the same object.
Your answer
Follow this Question
Related Questions
need help with locking the camera in Pause Menu 1 Answer
Pausing Music 1 Answer
Need help getting the screen to fade. 2 Answers
How to Appease a pause menu in a game that changes it's time scale? 1 Answer
Escape key not working 1 Answer