Problem with my 2d game

Hello everyone,

I hope you’re doing well. I’m reaching out today because I’ve been stuck on an issue for hours and could really use some help. I’m working on a 2D game in Unity, where the player progresses through different levels. Right now, I have two scenes set up, one for each level.

Initially, when I launch the game, everything works as expected. However, after the player dies, the “Game Over” menu I created appears correctly. But when I click “Retry,” the level restarts and my character gets duplicated — there are suddenly two instances of the character in the game. The same thing happens with the health bar, where there are now two bars stacked on top of each other. Additionally, after retrying, the “Game Over” menu stops working altogether.

The strange part is that this problem only occurs in Scene 1 — Scene 2 works fine. I can’t seem to figure out what the difference is. What’s even more puzzling is that the issue seems random. Sometimes, when I run the game directly from Unity, everything works fine the first time, and retrying causes the bug. Other times, the bug occurs right from the first retry.

When I build the game and test it, the bug happens consistently on the first retry, but only in Scene 1. In Scene 2, everything works as expected. This leads me to believe that the issue isn’t with the scripts, since the same ones are running for both scenes. I can’t figure out why the bug happens in Scene 1 but not in Scene 2.

What’s more confusing is that when the bug happens, I don’t see a duplicate of the character in the hierarchy, even though it appears in the game. The same applies to other elements that use DontDestroyOnLoad. Additionally, when the bug occurs, the number of collectible items (coins, for example) gets overlapped. It seems like this bug is causing several issues.

I really appreciate you taking the time to read this, and any advice or solutions would be greatly appreciated! Thanks again and I look forward to hearing from you.

Sounds like a defective singleton inappropriately applied to the player.

Look for inappropriate use of DontDestroyOnLoad(); and don’t do that. :slight_smile:

If that’s not it, it’s just another bug and and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.