I’ve been looking for a way to restart a game from scratch, but all I can find is how to reload a single scene. I’m looking for something that will not only reload the scene, but also reset things like Time.time or anything marked as DontDestroyOnLoad.
Put simply, how do I create the same effect as closing and reopening the entire game application, but from code?
You can’t do this, also Time.time is the current time which will always go forward. What you can do is making sure to reset everything when you load the start scene. Even for objects marked as DontDestroyOnLoad you have the chance to reset things.
A typical DontDestroyOnLoad object as a singleton:
public static GameManager instance = null;
//first code to run
void Awake()
{
//singleton
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
//singleton, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject); //<- this makes OnDestroy() be called, be careful
return;
}
//the rest is done once only...
DontDestroyOnLoad(gameObject);
//...
}