Hello everyone. I want to aska question. I am making a basic game (actually my first game) and this game has a try again button. This game is in the same concept with Flappy Birds. I tried a lot of method, ways but I couldn’t. My last try is loading game scene again, but it didn’t work very well. When I click try again button scene is opening with MainMenu which I created for start, and it doesn’t disappear. Here is my code and please somebody help I’m really confused
public class GameManager : MonoBehaviour
{
public static GameManager instance = null;
[SerializeField] private GameObject mainMenu;
[SerializeField] private GameObject gameOverMenu;
private bool gameStarted = false;
private bool gameOver = false;
private bool playerActive = false;
public bool GameStarted
{
get { return gameStarted; }
}
public bool PlayerActive
{
get { return playerActive; }
}
public bool GameOver
{
get { return gameOver; }
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void PlayerCollided()
{
gameOver = true;
gameOverMenu.SetActive(true);
}
public void PlayerStartedGame()
{
playerActive = true;
}
public void EnterGame()
{
mainMenu.SetActive(false);
gameStarted = true;
}
public void ReplayButton()
{
gameOverMenu.SetActive(false);
gameOver = false;
SceneManager.LoadScene("Flappy Zombies");
PlayerStartedGame();
EnterGame();
}
And I forgot one point. After adding sceneManager.LoadScene, I am getting the MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it error
Persistency is what you are looking for, meaning you need to pass information (data) between scenes. The easiest way to go would be to create another class lets say MyPersistendClass that would look like this:
public class MyPersistendClass : MonoBehaviour
{
public static MyPersistendClass instance;
public bool restart;
void Awake(){ //singleton code
if(instance == null)
instance = this;
else
Destroy(gameObject) //make sure it is attached alone at an empty GameObject
DontDestroyOnLoad(this)
}
}
and change your code to this:
void Start(){
if(MyPersistendClass.instance.restart){
gameOverMenu.SetActive(false);
gameOver = false; //probably you don;t need any of this below they will be set on their initial status
PlayerStartedGame();
EnterGame();
MyPersistendClass.instance.restart = false //this you need
}}
and
public void ReplayButton()
{
MyPersistendClass.instance.restart = true;
SceneManager.LoadScene("Flappy Zombies");
}
well not the best way but it should give you an Idea how you should work. Now on your code the reason it does not work is that LoadLevel happens on next frame so any code after it will be executed in regards your current scene , “When using this SceneManager.LoadScene, the loading does not happen immediately, it completes in the next frame.” from docs. So that s why mainmenu will be active when you load the scene. Keep in mind that when you load a scene an object that persist will lose all references to the scene objects thats why i didn’t propose to make GameManager one, if you want to you need to assign each reference dynamicaly on runtime.
Also because your game is minimal you could just redraw the map(call it whatever) and reset the players position to start and not load the scene from start, in that case you don’t need the persistend class.
Cheers.
Note: if you have no clue what i am talking about bother to do the Unity Tutorials they explain Singleton and persistency in “depth”.
Note1: when you want to add more info in your question please use the Edit Button.
Is flappy zombie the scene name