Displaying the score on the Game Over scene

Hello All,

I’m trying to extend the singleton pattern to display the player’s score in the Game Over screen, so they can have a look at it before it disappears.

I added the “Game Session” prefab as like exists in my level scenes, and I can see the score text in the Scene view. When I run the game from the second level, I get to the Game Over scene, and the score is not being displayed. I can see that the value has been retained, but it’s just not visible on the screen.

I noticed with my two “Level” scenes that in the scene view, the score size is WAY out of whack with the other game objects, but when I play the levels the score is displayed right where it’s supposed to.

In my “Game Over” scene, everything looks to scale.

I am wondering if my displayed score is way off the screen, because of the comparison with the scenes where the score is visible?

  • Rob

Hi Rob,

Which scene is the GameSession being created in? If it isn’t the second, this could be your issue. Try running the game from the beginning and see if the GameSession GameObject is persisted through into the second level.

If you are trying to persist the canvas and UI Text object also they would be in the same position on the GameOver scene as in the level. If they are not, then they are not the same object, which could explain why you are not seeing the score, e.g. it can’t update the UI Text object because it isn’t the same one.

That means I’ll have to actually be good at my game!

J/K, I can set the breakable blocks to 1, on the first level.

A few other thoughts:

  • When I start playing on the second level, the score is displayed as expected.
  • The “Game Session” is a prefab that is in each level scene, and then I added it to the “Game Over” scene. My understanding was that one would get created during the Awake() function, and then get Destroyed() if one already existed.
  • The score text is part of the “Game Session” prefab, so maybe the one in “Game Over” (which I moved to the center of the screen) is the one being Destroyed().
  • If the above is the case, can I reposition the existing text to be in the center of the screen?

I played from Level 1, and go the same result. The “Rect Transform” of the score text had some large values for X and Y (like 593 or 480 something). With the game still running, sitting on the Game Over scene, the score is in the upper right hand corner of the Scene view (like it was in the two level scenes), but not displayed in Game view.

You shouldn’t need to add a GameSession to each scene. Typically you’d put it in the first scene that needs it and then the DontDestdoyOnLoad method will keep it alive between scene changes. Of course, this would mean you can just run up any specific level you want, as you are at the moment.

The singleton code in the course isn’t really ideal, a better solution would be one where when the singleton is accessed it creates it if it doesn’t exist, that would then enable you to run any level individually as required.

You should be able to achieve something similar using the course version but I suspect your issue at the moment is that the GameSession which is getting destroyed is the wrong one.

Happy to take a look if you’d like to share your project, but it might be a couple of hours before I have chance to respond at the moment.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Hey Rob,

Thanks for the offer of looking at my code! I tried to pare things down, but could only get to a 18MB zipped file without tearing up a bunch of stuff.

I did some digging and found this video session from a few years back that looks like it should work.

I’m not going the whole 9 yards and saving information to a file, though I did think about it for having a leader board feature. There are ALWAYS additional features that could improve the game, but I feel like this is a minimum for player enjoyment. They should know their score when they’re done!

After I get this and possibly implement having more than one “life”, I will keep progressing in the course. :slight_smile: Maybe I’ll get time to come back to the leader board.

I’ll update my original post to let everyone know if I’ve solved it with what I propose above.

  • Rob

I was able to refactor my project to get this working. Here is what I had to do:

  • Get rid of the “Game Session” prefab, because it had a canvas component to it
  • Make the “currentScore” member of the GameSession class public and a non-SerialezedField
  • Remove the TextMeshPro member of the GameSession class and its associated code to populate it
  • Create a script, DisplayScore.cs that converted the currentScore to a TextMeshPro string and attach the script to the camera in each of the level scenes and the “Game Over” scene
  • Add a canvas to each level that contained a TextMeshPro text component and connected the component to the “scoreText” member of the DisplayScore class.

The score is now displayed on the “Game Over” scene when the player loses, which happens a lot!

I think the main feedback that I’ll receive is that the levels are too difficult. I haven’t been able to beat one yet. It seems like I shouldn’t push it out to my testers without fixing that. We’ll see.

  • Rob
2 Likes

Well done Rob, much more satisfying when you can work through an issue yourself and get the results you wanted. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms