Score increments only in first level

Hey @Rick_Davidson, the lecture is great - as they’ve all been thus far - and I think I’m missing something subtle.

My build settings have level 1 and then level 2. If both scenes contain GameStatus->Game Canvas->TextMeshPro Text, then the score increments fine in L1, however it won’t increment in L2 (as in the score at the end of L1 displays in L2 no matter how many blocks I break).

As you might guess, if I delete GameStatus from L2, then the score increments just fine, but that defeats the purpose of this lecture.

I logged the current score to the console inside the AddToScore() method, and it does indeed run when I break blocks in L2, however it outputs the same score each time.

Things I’ve done which result in the same behavior:
–Verified that the Awake() module in the GameStatus script is identical to what you show in the lecture.
–Deleted all objects from L2 and rebuilt the hierarchy from prefabs.
–Reversed the order of L1/L2 in the build settings - no matter the order, the level which plays 2nd will not increment the score.

Any ideas?

Update: I happened to Reimport All for my prefabs, and the score worked properly for a brief period of time - maybe a half-dozen play-throughs after that point. At some point, with me having changed no code, and only moved a few blocks around, it’s back to not incrementing the score on the level which plays second.

1 Like

Hi @Chuck_Story, thanks for the question. Can you please also ask this over on the Udemy Q&A section - I think Nina will be able to give you some good insight over there.

Did you ever find a solution to this? I’m having the same problem and couldn’t find the thread on the Udemy Q&A. I saw another thread on this message board with another method of implementing the singleton pattern that guards against multiple instances then FindObjectsOfType runs, but I can’t tell if that works for this problem.

Hi @Chuck_Story and @Rick_Davidson,

I think I found a fix for this. As near as I can tell, Unity wasn’t treating this script as a true singleton. To get around that, I declared the player score and the scoreboard as static variables. This might make the reset a bit more complicated, but it seems to be working now.

In order to get Unity to recognize the scoreboard as static, I had to serialize a field, then assign that field to a private static variable.

Here’s my code:

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

public class GameStatus : MonoBehaviour {

    [SerializeField] [Range(0.1f, 1.0f)] float gameSpeed = 1.0f;
    [SerializeField] TextMeshProUGUI scoreText;

    static int playerScore;
    static TextMeshProUGUI scoreboard;

    private void Awake ()
    {
        int gameStatusCount = FindObjectsOfType<GameStatus>().Length;
        if (gameStatusCount > 1) {
            Destroy(gameObject);
        } else {
            DontDestroyOnLoad(gameObject);
        }
    }

    void Start () 
    {
        scoreboard = scoreText;
        scoreboard.text = playerScore.ToString();
        Debug.Log("Current score is " + playerScore.ToString());
    }
	
	// Update is called once per frame
	void Update ()
    {
        Time.timeScale = gameSpeed;
	}

    public void AddToScore (int points)
    {
        Debug.Log("Adding " + points.ToString() + " points to score.");
        playerScore += points;
        Debug.Log("New score is: " + playerScore.ToString());
        scoreboard.text = playerScore.ToString();
    }
}
1 Like

I found a thread on the Udemy Q&A section (Lecture 69) with the same problem, using DestroyImmediate() instead of Destroy() was the solution there.

9 Likes

Thanks Vizibal for taking the time to add this comment here - been scratching my head for a couple of hours with this one. Much appreciated.

1 Like

Vizibal - thanks, that fixed it. Was working around with a hacky check in Update to re-grab the component if my caching variable was null. This is much better.

Thanks Vizibal. I was having an issue where the blocks on following levels would end up with a null reference to the GameStatus. I suspect the blocks were running their Start() and assigning the reference before their scene’s GameStatus finished destroying itself. Once the Destroy() finished, all the blocks would be pointing to nothing. I tried a dozen different things with no joy but switching to DestroyImmediate() solved the problem! You saved my sanity, or what’s left of it.

Thanks Vizibal for helping us .

Yeah it is working.

Privacy & Terms