The score is not changing ( Block Breaker)

So, I tried a lot of fixes but it is not fixing. When I start the game, the blocks are getting destroyed but the score is not updating and I get a null error even though I tried the fix Rick had showed.
The video link is below -
https://drive.google.com/file/d/1tbKFaQIe0k5LCvL0sd098FohSFGI5w_M/view?usp=sharing

The Block script code -

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

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

    Level level;

    private void Start()
    {
        level = FindObjectOfType<Level>();
        level.CountBreakableBlocks();
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        DestroyBlock();
    }

    public void DestroyBlock()
    {
        AudioSource.PlayClipAtPoint(breaksound, Camera.main.transform.position);
        Destroy(gameObject);
        level.BlockDestoyed();
        FindObjectOfType<GameStatus>().AddToScore();

    }
}

The GameStatus script Code -

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 = 83;
    [SerializeField] int currentScore = 0;
    [SerializeField] TextMeshProUGUI scoreText;

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


    public void Awake()
    {
        int gameStatusCount = FindObjectsOfType<GameObject>().Length;
        if (gameStatusCount > 1)
        {
            gameObject.SetActive(false);
            Destroy(gameObject);
        }

        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }





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

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


}

Unity screenshots -




please help me I am stuck with this for a long time

    public void DestroyBlock()
    {
        AudioSource.PlayClipAtPoint(breaksound, Camera.main.transform.position);
        level.BlockDestoyed();
        FindObjectOfType<GameStatus>().AddToScore();
        Destroy(gameObject);

    }

I’m not sure, but maybe you can try this.
it gets executed from top to bottom, so if you destroy the block, whats below wont be executed.
I’m also still learning, but cant hurt to try ;p

Hi @asmim_haque,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Double click on the error message. To which line in your code does it refer?

Yes, I compared my code with the Lecture Project Changes, it is the same and also I clicked on the link and went to the line but I couldn’t find any mistake
The screenshot below -


Is there a GameStatus object in your Hierarchy in the active scene? Check that while your game is running.

When I start the game the GameStatus disapears

Check if there are multiple GameStatus objects in your Hierarchy. For simplicity, you could add a Debug.Log to the Start method in the GameStatus class and log the name of the game object to which your GameStatus component is attached into your console. Alternatively, check each game object manually.

No, I only have 1 game status script attached to the game status game object.

How did you verify that there is only one game status script in your entire scene? Did you use a Debug.Log in the Start or Awake method?

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

Privacy & Terms