About 'Implement Singleton Pattern'!

In this video (objectives)…

  1. Introduce the Singleton Pattern.
  2. Use DontDestroyOnLoad().
  3. Examine the Unity script execution order to see that we can use Awake() for our singleton.

After watching (learning outcomes)… Able to implement a Singleton Pattern to ensure that our score persists across level load.

(Unique Video Reference: 26_BR_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!

Hi! I love the course so far; it is great to find a course that doesn’t just run through endless boring exersizes on for loops and whatever. It really seems to get me on the way!

but after this lecture I got stuck on something. I believe I followed every step you took, but somehow I have a problem. My score does increase in the level that I played the run button in, and after finishing the level the game goes to the next scene, keeps the game status gameobject, destroys a game status gameobject, and continues playing. But in this second level, the score does not increase! it keeps the end score of the previous scene (83, since there was only one block). I let it go to game over, start new game, and I am back in my first scene. and there my score DOES increase again (with 83 points) and goes to the next level.

This is the same if I start with my second scene. Only in the scene that i started out with, the score increases…

any thoughts on what might be the problem? I’m pretty lost haha.

thank you so much in advance!

the code of my gamestatus:
> using System.Collections;

using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class GameStatus : MonoBehaviour {

    // config params
    [Range(0.1f, 10f)] [SerializeField] float gameSpeed = 1f;
    [SerializeField] int pointsPerBlockDestroyed = 83;
    [SerializeField] TextMeshProUGUI scoreText;

    // state vars
    [SerializeField] int currentScore = 000; // serialized for debugging

    private void Awake()
        int gameStatusCount = FindObjectsOfType<GameStatus>().Length;
        if (gameStatusCount > 1)

    private void Start()
        scoreText.text = currentScore.ToString();

    // Update is called once per frame
    void Update () {
        Time.timeScale = gameSpeed;

    public void AddToScore()
        currentScore += pointsPerBlockDestroyed;
        scoreText.text = currentScore.ToString();

the code of my block:
> using System.Collections;
> using System.Collections.Generic;
> using UnityEngine;
> public class Block : MonoBehaviour {
> [SerializeField] AudioClip breakSound;
> //cached refs
> Level level;
> GameStatus gameStatus;
> private void Start()
> {
> level = FindObjectOfType();
> level.CountBreakableBlocks();
> gameStatus = FindObjectOfType();
> }
> private void OnCollisionEnter2D(Collision2D collision)
> {
> DestroyBlock();
> }
> private void DestroyBlock()
> {
> AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);
> Destroy(gameObject);
> level.BlockDestroyed();
> gameStatus.AddToScore();
> }
> }

Fixed it!
I realise I did not always follow the steps :wink: have been wondering why in this case you used
instead of the whole ‘declare ref at the beginning, find the object of that type in start, and use the reference’ procedure. so I tried it out, and it worked. But appartently it also caused the jam!

so the problem is solved, but then I have a new question:
Why does this go wrong? Does the ‘start’ method of the blocks act before the ‘awake’ function of the game session gameobject? is that a known problem?

kind regards,

I have the FindObjectOfType in Start and it works for me. Looking at your code I have a question: is there a problem if you Destroy(gameObject) before calling level.BlockDestroyed() and gameStatus.AddToScore()? Perhaps the Block is destroyed before calling it?

I have tried it but my game works fine.

Hi guys, I’ve a problem. I added the Awake method in GameStatus.cs but I can’t see the changes that Rick is saying at the 11th minute of the video.

Do you know why?

This is the error that I have (only when I start the game and the ball is hitting a block): “NullReferenceException: Object reference not set to an instance of an object Block.DestroyBlock () (at Assets/Scripts/Block.cs:27)
Block.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/Block.cs:22)”

If I put the Awake method between /* … */ everything works fine like the previous video.

Privacy & Terms