So I haven’t continued watching the video but I just did the mega challenge and it works fine for me! I want to show you my script and see if you agree and if you guys have any things that I need to change!
Below is my Level script which is inside of a Level GameObject which is prefabbed in all of my two levels.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Level : MonoBehaviour
{
public static int breakableBlocks;
[SerializeField] GameObject blocks;
private void Start()
{
breakableBlocks = blocks.transform.childCount;
print(breakableBlocks);
}
public static void BlockBroken()
{
if (breakableBlocks > 0)
{
breakableBlocks -= 1;
print(breakableBlocks);
}
}
private void Update()
{
CheckIfAllBlocksBroken();
}
private void CheckIfAllBlocksBroken()
{
if (breakableBlocks == 0)
{
LoadNextLevel();
}
}
private static void LoadNextLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
Here is my Block script where when a collision is registered, it calls the BlockBroken method above:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class Block : MonoBehaviour
{
[SerializeField] AudioClip breakSFX;
private void OnCollisionEnter2D(Collision2D collision)
{
AudioSource.PlayClipAtPoint(breakSFX, Camera.main.transform.position);
Level.BlockBroken();
Destroy(gameObject);
}
}