- Home /
How should I reset my game between each try ?
Hello,
I wonder how I should reset my game when my player die. Can I just reload the scene, to reset all gameobject easily ? I can't just Tp my player to the beggining of the parkour because my game is a verticale Plateformer with a random parkour. So in my opinion the best option would be to reload the scene to restart. But is there any undesirable effect to that option ? Anything I should know ? It's quiet an important part of my game to restart it fast and I need to restart it every 2 to 5 minutes. I can reset my LevelManager, my BackGround Manager and my GameManager and tp my player, would it be a better option ? The game is meant to be played on a phone, so I would like to have something a minimum optimize. =)
Answer by Chrissyeah_ · Sep 24, 2020 at 03:55 PM
Use Namespace using UnityEngine.scenemanagement
and in your code SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
to reload the scene. Reloading the scene will set it to the state it was last saved as. So it should work fine, to my knowledge the main drawback of this method is any saved data you may want.
Perhaps there are enemies you kill that you don't want to kill again, or items you picked up you don't want to have to pick up again.
But if the goal of the level is simply to get from A to B with the same system each time. Loading the level should be fine.
Okay, thanks for the answer ! I'll just have to save my data before ! I know how to do that !
I believe application.loadlevel is outdated in the newer versions of unity, so it doesn't work add "using UnityEngine.scenemanagement" at the top of your script and add Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex); where you want to reload your scene.
Yep, just did that and it worked ! Just have one more slight problem, my music is also restarting, and that's normal I know, but it wasn't planned ^^
Answer by unity_Z4478tN91mRAQQ · Sep 24, 2020 at 04:32 PM
Just have another question for you guys, I used DontDestoyOnLoad(gameObject)
to save my AudioManager, like that the music won't restart every time, but how should I do to make sure that a new AudioManager won't reload in the new scene ? Because if I keep it like that, I have 2 AudioManager and 2 times the same music, unsync of course, and it's a bit annoying, I think you'll agree with me =)
Solved with : private static Audio$$anonymous$$anager _instance;
void Awake()
{
if(_instance == null){
_instance = this;
DontDestroyOnLoad(gameObject);
//Some stupid code that I hate but I'm too lazy to change(put all of what you have to put in Awake here)
}else{
Destroy(this);
}
}
I'm glad you came right, another option could be to instantiate it only if it's not found, so if it's a component on a gameobject you could do if (!GetComponent<>())
or you could assign it it's own tag and use if (!GameObject.FindGameObjectWithTag("foo"))
Answer by l3mon · Sep 24, 2020 at 06:26 PM
I would recommend to split the scenes. You can have a basic scene with one audio manager, which loads your game scene additionally. Then you just unload and reload the game scene. That way, the basic scene stays as is.
Your answer
Follow this Question
Related Questions
My rigidbody2d is passing through the side of a collider2d but it's working on the top 0 Answers
Teleport Not Working in Unity 2D 1 Answer
I know not how to implement the "open IAB" in unity 0 Answers
Android 2D Character Controller 2 Answers
Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers