0

In my game I have two panels in one scene (two gameobjects);

  1. Title Screen (gameobject)
  2. ActiveGame Screen (gameobject)

When I press a play button in Title Screen, the active (Title Screen) gameobject becomes passive. ActiveGame Screen is active and the game starts directly.

To start the game from the beginning, I have to activate the title and enter the main menu, then press the start button. To restart, I use this code:

public void Restart()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

Is it possible to reload the gameobject Activegame Screen even if it is still active?

derHugo
  • 83,094
  • 9
  • 75
  • 115
Maxpayne
  • 1
  • 2

1 Answers1

0

May we see your hierarchy and your "scenes" folder? I think you did put your title screen and your game in the same scene. You should have separate it into different scenes and then load another one using LoadScene(sceneName);. It will be hard to make a restart when your title screen and your game is in the same scene. Try creating new scene and moving your Activegame Screen gameObject into it. Then add a function public void game(){ SceneManager.LoadScene(game); } then call this function when your "start" button or something pressed.

To restart your game scene that you will (I hope) create in the future, you should use that code you provided above.

To know more about unity scenes Go here About unity scene manager Go here

Hopefully I was helpful. Good luck!


Edit

Since you already prepared everything you will have to make it hard way because there's no turning back. Time to bring the heavy guns. My favorite. A PlayerPrefs!!! It stores variables but not like your regular ones. It can keep those even if you exit the game and stuff.

Here you will use it to see if the scene is loading from the restart button or from the somewhere else.

In that reset function you provided above add this line before LoadScene thing:

PlayerPrefs.SetInt("FromReset",1);

Now it the "FromReset" variable will be 1 whenever we call that function. It's time to know if scene is from reset or not. Go to your game manager script or something like that where you activate and deactivate gameObjects. In Start add these lines:

if (PlayerPrefs.HasKey("FromReset"){
if (PlayerPrefs.GetInt("FromReset")==1){
PlayerPrefs.SetInt("FromReset",0);
//or
PlayerPrefs.DeleteKey("FromReset");
//Reset start
}else{
//Normal start
}else{
//Normal start
}

Now it should work in theory. More information about Playerprefs you can find HERE

YGreater
  • 42
  • 1
  • 11
  • Thank you for your help. I've tried your suggestion. But I have a GameManager and if I split my screens as scene, GameManager doesn't work. Here's my main scene hierarchy https://imgur.com/rgg9Hjg – Maxpayne Jul 17 '22 at 19:56
  • Got it. So I have one idea on how to do it. I have updated my answer. Please go ahead and try that. and let me know if it works. Now I will go to sleep with a hope that it will help. Best of luck – YGreater Jul 17 '22 at 20:19