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!