About 'Add & Display Score'!

In this video (objectives)…

  1. Create GameSession.cs to handle our score value and updates.
  2. Create public methods for other scripts to call to influence score.
  3. Display score on Game screen and Game Over screen.

After watching (learning outcomes)…

Display the player's score on the game screen and game over screen.

(Unique Video Reference: 31_LD_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

Rick,
Be great, at this point, to spend some time discussing Object vs. Objects, what the difference is, when and where. Some may discren the the useage one their own. But you have multiple times pointed it out as a possible Doh!! moment. And I think the course would benifit from a few minutes of specific explination on this.
Thanks so much for all the effort!!

2 Likes

Great point, I’m showing which one to use, and not explaining the underlying why they are different.

1 Like

Hi Rick

How did you fix the error at the end? (112. Add & Display Score 19:10)

An error occurs when you play the game at “Start Menu”
If you play the game from the “Game” scene then there is no error.

Thanks

2 Likes

Hi
Did you solve this issue?

What is the error that you are both referring to?

I am sure it’s something I missed, but when I click “Start” from the start Menu, I get:
NullReferenceException: Object reference not set to an instance of an object
Level.LoadGame () (at Assets/Scripts/Level.cs:19)

Its trying to destroy a gameObject that doesn’t exist. If I start from the game or even Game Over scene then all functionality works perfectly. Still digging into what I have done wrong.

1 Like

Hi Ian,

Ok, thanks for the info… I suspect this is going to be a reference to some form of GameSession Object at a guess, but out of interest, what is the code on line 19 of Level.cs (copy/paste the whole script up if its easier).


See also;

Its the

FindObjectOfType<GameSession>().ResetGame();

which in turn calls:

public void ResetGame()
    {
        Destroy(gameObject);
    }

Hi Ian,

Ok, so what is happening is that there is no longer a GameSession object in the scene, the reference to it is null and you are then trying to access one of the object’s members, ResetGame which cannot be accessed.

At what stage do you first create the GameSession object, I’m guessing its using a singleton pattern, and as such destroys an duplicates that are created? Do you start with one in the StartMenu scene, or, is the first time it’s created in the Game scene?

1 Like

It happens because when you hit start at the first time there is no game session to reference, so it shows NullReference

public void LoadGameScene()
    {
        SceneManager.LoadScene("Game");
        FindObjectOfType<GameSession>().ResetGame();
    }

I set an if condition to solve the problem:

public void LoadGameScene()
    {
        SceneManager.LoadScene("Game");
        if (FindObjectOfType<GameSession>())
        {
            FindObjectOfType<GameSession>().ResetGame();
        }
    }
5 Likes

Cheers for identifying that problem for me champ…I was thinking this was the problem

dude you are a bloody legend

Hello everyone, this is the first time I have intervened, but I have a big doubt: I can not understand the difference when using FindObjectOfType and FindObjectsOfType.

Thank you for any help

Yeah that can be a tricky one! One is singular (finds just one thing of that type, usually the first one it comes across) and one is plural (finds all of the things of that type and allows you to store those things somewhere such as in an array).

So if you want to find the player, typically there is just one and you’d use FindObjectOfType. Typically we don’t use these Find methods in Update because they are expensive, but using them in Start or at a specific time is fine.

Thanks Rick. I’m learning a lot with this course.
And on the other hand I congratulate you. You are one of the best teachers I have ever had, including those from my university career.

2 Likes

Thanks you for your kind words!

Privacy & Terms