The last block destroyed doesn't affect the score

After finishing a level, the last block that I destroy before loading the “Game Over” scene doesnt affect the score. It’s supposed to add 15 to the score, but doesn’t. Is there a way to fix this?

1 Like

Hi! Welcome to the community!

If everything works correctly, the only thing that comes to mind that might be causing this is that your code loads the scene before adding the score, but can’t be sure unless you copy-paste your code in here.

Here’s my code:

public class Block : MonoBehaviour
{
[SerializeField] AudioClip breakSound;

//cached reference
Level level;
GameStatus gameStatus;

private void Start()
{
    level = FindObjectOfType<Level>();
    gameStatus = FindObjectOfType<GameStatus>();
    level.CountBreakableBlocks();
}
private void OnCollisionEnter2D(Collision2D collision)
{
    gameStatus.AddToScore();
    AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);
    Destroy(gameObject);
    level.BlockDestroyed();

}

}

level.BlockDestroyed(); is linked to the loadNextScene() function or script

1 Like

Sorry to bother, But could you also copy-paste your gameStatus and level scripts?

gameStatus :

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

public class GameStatus : MonoBehaviour
{

[Range (0.1f,10f)][SerializeField] float gameSpeed = 1f;
[SerializeField] int pointsPerBlockDestroyed = 15;
//state variables
[SerializeField] int currentScore = 0;
[SerializeField] TextMeshProUGUI scoretext;

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

void Start()
{
    scoretext.text = currentScore.ToString();
}

// Update is called once per frame
void Update()
{
    Time.timeScale = gameSpeed;
    scoretext.text = currentScore.ToString();
}

public void AddToScore()
{
    currentScore += pointsPerBlockDestroyed;
}
public void DestroySelf()
{
    Destroy(gameObject);
}

}


level :

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

public class Level : MonoBehaviour
{
[SerializeField] int breakableBlocks; //serialized for debugging
SceneLoader sceneloader;
private void Start()
{
sceneloader = FindObjectOfType();
}

public void CountBreakableBlocks()
{
    breakableBlocks++;
}
public void BlockDestroyed()
{
    breakableBlocks--;
    if (breakableBlocks <= 0)
    {
        sceneloader.LoadNextScene();
    }
}

}


sceneLoader (just in case you need it) :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
public void LoadNextScene()
{
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(sceneIndex + 1);
}

public void LoadStartScene()
{
    SceneManager.LoadScene(0);
    FindObjectOfType<GameStatus>().DestroySelf();
}

public void Quit()
{
    Application.Quit();
}
1 Like

I’m not 100% sure if Rick will address that issue later on in the course, he probably will because it’s kind of a fun mistake that will cause the game to behave weirdly after adding some extra levels, it might even be a challenge, so keep reading at your own risk.

Cilck here for solution

You are actually adding the score but the UI isn’t updating because it doesn’t get a chance to do so before changing scenes, if you had another level you would notice that the first block destroyed would give you double the points due to that, making the bug kinda fun in a way.

To fix that you just have to move this line of code inside the Update method, scoretext.text = currentScore.ToString(); to your AddToScore function.

1 Like

Thanks a lot man!

1 Like

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