The game scene does not stop

Hi,

There is a game scene and a menu scene. I can switch from game to menu with SceneManager without any problem. However, when I start the game again from the menu, it should start again, but the game continues from where it left off. I have an issue where the game scene doesn’t stop and keeps running in the background. Can you help me?

Is this in the Editor? The game will start with whatever Scene you choose to load up first when it’s actually compiled.

What metric makes you think it is continuing?

The score going up? Are you using a static variable for the score? If so, set it to zero when you start a new game.

The time going up? Are you using Time.time? Look for why it is not resetting, and instead use your own float variable for time, advancing it by Time.deltaTime yourself.

Something else? Here’s how to find it:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

First of all, thank you for your attention @Kurt-Dekker & @RadRedPanda . There is a “Start” button in the “Menu” scene with number “0”. While on this stage, I start the game via Unity and press the “Start” button on the stage. The button takes me to the “Game” scene number “1”. When the stage is opened, I see the moving enemies hit my character and his health is reduced by 1 (the game was set to stop automatically when each health decreased). This means that when I start the game while in the “Menu” scene, the “Game” scene is also running in the background. There is a problem as if all the scenes are running automatically when I press the “Play” button on Unity. So when I switch to the game scene from the menu, the game does not start over. I find it started earlier and the character’s health is reduced by 1.

I’m sorry, I still don’t really follow your explanation. Are these actually two different scenes? And if so, you’re saying the Game Scene is still there while the Menu Scene is also open? Loading a new scene will destroy all of the objects in the old scene, unless you’ve specifically set them to DontDestroyOnLoad, even when loading a scene you’re already in.

If you’re using ScriptableObjects for your health value, then it won’t reset, as ScriptableObjects keep their values between scenes. You would need to have some script on Start to reset the health value.