- Home /
DontDestroyOnLoad gameobject once destroyed is not reloading. How to reset scene to allow it reload?
My game has a gameobject that has a DontDestroyOnLoad method. I also have a pause menu that can access the main menu which is in a difference scene. My problem is that if I access the main menu from the pause menu then the gameobject carries over into the main menu scene. Yet if I destroy the gameobject, when clicking the new game button which loads the main scene then the gameobject is still destroyed. How would I go about resetting my main scene when I access it from the main menu so then the gameobject is created again? Thank you.
Answer by FlaSh-G · Sep 06, 2017 at 10:58 AM
Why would you destroy the first GameObject anyway? If I understand you correctly, you simply don't want two of these GameObjects to exist. Why would you destroy the first one and try to use the new one if you could just destroy the new one and keep the old one?
private static NameOfThisClass singleton;
void Awake()
{
if(!singleton)
{
singleton = this;
DontDestroyOnLoad(this);
}
else
{
Destroy(gameObject);
}
}
(This pattern only is viable if you really plan to only ever have one of this kind of script at a time.)
What I don't want is for the gameobject to be seen/shown on the main menu screen. $$anonymous$$y game starts on the main menu, you press the new game button and it takes you to first scene of the game where the gameobject which is the player is. From here if you press escape, a pause menu shows which has the option to go back to the main menu. On returning to the main menu the player/gameobject follows as it has don't destroy on load method. What I don't want is for the gameobject to come here and when I press new game I want the player/gameobject to reload e.g all of it stats back to what it would be on a new game
To me, this sounds a lot like you don't want to DontDestroyOnLoad the player. Whatever player related data you want to have between scenes, you can carry it along without DontDestroyOnLoad. With a/some static variable(s), for example.
Your answer
Follow this Question
Related Questions
Why are my Game Objects being destroyed? 0 Answers
How do I get a material from a GameObject? 1 Answer
Character animation problem 1 Answer
objects in the same array wont move 0 Answers
Transport objects between scenes. 1 Answer