Hi, please help! I have been looking through other people’s problem about this and I have checked the Script box for Blocks already and still nothing happens when I play the game. Also I couldn’t find typing mistakes. I have been trying to look for a solution but still no luck. The number of blocks stays in zero, so as soon as I hit any block the level changes. I include images of my game in Unity and the script for Block and Level.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Block : MonoBehaviour
{
[SerializeField] AudioClip explosionSound;
[SerializeField] GameObject blockSparklesVFX;
//Cached Reference
Level level;
private void Start()
{
CountBreakableBlocks();
}
private void CountBreakableBlocks()
{
Debug.Log("CountBreakableBlocks method called");
level = FindObjectOfType<Level>();
if (gameObject.tag == "Breakeable")
{
Debug.Log("Breakable tag detected");
level.CountBlocks();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (tag == "Breakable")
{
DestroyBlocks();
}
}
private void DestroyBlocks()
{
PlayBlockDestroySFX();
Destroy(gameObject);
TriggerSparkleVFX();
level.BlockDestroyed();
}
private void PlayBlockDestroySFX()
{
FindObjectOfType<GameSession>().AddToScore();
AudioSource.PlayClipAtPoint(explosionSound, Camera.main.transform.position);
}
private void TriggerSparkleVFX()
{
GameObject sparkles = Instantiate(blockSparklesVFX, transform.position, transform.rotation);
Destroy(sparkles , 1f);
}
}
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<SceneLoader>();
}
public void CountBlocks()
{
breakableBlocks++;
}
public void BlockDestroyed()
{
breakableBlocks--;
if (breakableBlocks <= 0)
{
sceneLoader.LoadNextScene();
}
}
}