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 -