Reset button in an one scene game
[It's a HUGE topic because I'm also describing my thought process throught the entire failed experiment lol, sorry about that]
Ok, so I've been searching for an answer on this one for over a day and I'm getting a little frustrated lol, so I decided to cry for help.
Thing is: I'm taking that Udemy Course on Unity Game Dev (and I think I should add here that I have ZERO knowledge in ANY kind of programming language WHATSOEVER), and I'm creating this Flappy Bird like game with this flying zombie and all that. So, we have this floor that is moving from right to left, these obstacle-like rocks that are also moving right to left but also from the top to the bottom of the screen, and the player that jumps every time you hit the mouse button, and, if in contact with any object, he falls down and dies.
Since it's a really simple game, there is no scene change, and the Main Menu and the Game Over screen is on the same scence.
So thing is: I wanna add this Replay button to the Game Over screen that, when pressed, will bring the player back INTO the game, and not to the Main Menu. I thought to add a function to the Restart button that would get it back to EnterGame function, and it kind of worked -- except the player was nowhere to be found (probably because he felt throught the void). Also, by doing this, unexpectedly, the game went back not to the start, but to the place the player was when he hit the obstacle.
So I thought that what I should do is reload the entire scene, because the player would be in the starting position, as it should be. And it did work as it should, but the problem is it ALWAYS restarts in the main menu, and I can't seem to be able to turn this object off after the reload, so that when you press restart the game starts over (there is a "main menu" button on the game over screen and that's working perfectly with the reload scene thing).
Now, while I do think that creating a new scene to the menus could probably be an easier way to fix this problem, if there is any chance to do so without adding a new scene I'd much like to understand the logic behind it (after this problem is solved I might as well try to fix this problem by creating a new scene just for the kick of it).
So there are 4 C# scripts: one that controls the objets moving from right to left, one daughter script to the moving rocks that adds the up-and-down movement of these obstacles, one that govern the player (making him jump as you press the mouse button and that tells the player he died when he hit an obstacle) and the Game Manager. I think I should only post the Game Manager here, but if you guys thinks otherwise tell me and i'll post the other ones too.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public static GameManager instance = null;
[SerializeField] private GameObject mainMenu; // I attached an object called MainMenu to this field. In this object we have the buttons and the camera for the Main Menu
[SerializeField] private GameObject gameOverScreen; // Same thing, but with the Game Over Screen
private bool playerActive = false; // This is to tell the objects to just start moving when the player "jumps" for the first time
private bool gameOver = false;
private bool gameStarted = false ;
public bool PlayerActive
{
get { return playerActive; }
}
public bool GameOver
{
get { return gameOver; }
}
public bool GameStarted
{
get { return gameStarted; }
}
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
Assert.IsNotNull(mainMenu);
}
void Start()
{
}
void Update()
{
}
public void PlayerStartedGame ()
{
playerActive = true;
}
public void PlayerCollided ()
{
gameOver = true;
}
public void EnterGame() // This function is attached to the "Play Game" button on the Main Menu
{
mainMenu.SetActive(false);
gameOverScreen.SetActive(false);
gameOver = false;
gameStarted = true;
}
public void GameOverScreen ()
{
gameOverScreen.SetActive(true);
}
public void BackMain() // This function is attached to the "Main Menu" button on the Game Over screen
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void RestartGame() // THIS IS THE BUTTON THAT IS NOT WORKING AS I INTENDED TO!!!!!
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
mainMenu.SetActive(false); // I thought that if I reloaded the Scene and deactivated the Main Menu it would start directly into the game, but that's not what's hapenning. It goes back to the main menu nonetheless.
}
}
So there it is. Any thought on where my logic was wrong?
ALSO if possible, the Game Over screen comes immediately after the player touching the object, missing the player's death sentence. I'm thinking of using a Coroutine to only call the game over screen after one second. I do realize that I have to tackle one problem at a time, but do you guys think this is the way to go? or is there a simpler way?.
GEE, I'M SORRY FOR THE HUGE TEXT and the English language problems I'm sure that are there, but if you guys could help me on that I'd sure appreciate!
As we say in spanish. This is "infumable" (non smokable...) So long. Please read the FAQ
What kind of questions can I ask here?
Unity questions, of course! As long as your question is:
detailed and specific
written clearly and simply
of interest to at least one other Unity user somewhere
Remake it please, or commence with a short explanation
Your answer
Follow this Question
Related Questions
Slow scene 0 Answers
Scene Reset on HTC Vive Idle? 0 Answers
cursor disappears in GameOver scene 0 Answers
I'm making a 2D game with no levels like flappy bird. How many scenes will I need? 1 Answer
How do I restart a scene on collision? 0 Answers