Score increases even when hitting walls

Hi,

In my Brick Breaker game, the score increases 1 point even when colliding with the walls of the screen, however, it does not add 1 when colliding with the paddle. The game also adds 1 to the score automatically when it runs. Unsure if that can be related.

Here is my code for Game Status

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


public class GameStatus : MonoBehaviour {

    //Configuration parameters
    [Range(0, 10)] [SerializeField] float gameSpeed = 1f;
    [SerializeField] int pointsPerBlockDestroyed = 1;
    [SerializeField] TextMeshProUGUI scoreText;

    // State Variables
    [SerializeField] int currentScore = 0;

    private void Start()
    {
        scoreText.text = currentScore.ToString(); //within the scoreText we are looking to access text
    }
    // Update is called once per frame
    void Update () {
        Time.timeScale = gameSpeed;
	}

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

And here is my code for the Block

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


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

    Level level; //cached reference

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

    private void OnCollisionEnter2D(Collision2D collision)
    {
        DestroyBlock();
    }

    private void DestroyBlock()
    {
        FindObjectOfType<GameStatus>().AddToScore();
        AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);
        Destroy(gameObject);
        level.BlockDestroyed();
    }
}

Thanks!

Hi,

Is a Block script attached to a wall?

No, all the wall’s inspectors look like the following image.

image

Add the following line to Block.cs:

    private void Start()
    {
        Debug.Log("Block.cs attached to: " + gameObject.name, gameObject);
        level = FindObjectOfType<Level>();
        level.CountBreakableBlocks();
        
    }

If you see in your console that the Block component is not attached to a block, click on the message once. The corresponding game object will get highlighted in your console.

Do you call AddScore() in other classes but Block?

Hi,

As it has been two weeks since a response on this can you please update if it has been solved.
Topic will lock within 3 days so please make another post or reference this one if the issue still persists.

Thanks

Thanks for this, so when I do that, I get the back that it’s attached to Block and Ball.

Also, I’ve noticed my score always starts at 0, and adds 1 additional point when switching scenes. Unsure if that can be related. If not, you can ignore it.

image

Thanks!

The Block.cs is not supposed to be attached to the ball game object.

1 Like

This topic was automatically closed after 4 days. New replies are no longer allowed.

Privacy & Terms