Next Level not Loading after Breaking All Blocks

cud_s05_statics_to_detect_win_condition

#1

Hello,

I am not quite sure what I am doing wrong, but I think it has something to do with Brick.breakableCount and its application in the BrickDestroyed() method. I’ve noticed that the videos for Brick Breaker are not quite applicable with the newest versions of Unity because Application.LoadLevel is obsolete. I have been using the SceneManager.LoadScene without any issues up until this point and now I can’t seem to figure out what’s going on. When I log the number of bricks left to be broken, breakableCount is definitely working but when breakableCount hits 0, the next scene doesn’t load. I am not getting any errors in my code either which makes it hard to determine where the error is arrising.



#2

Hey, you may have figured this out already… but I noticed that your HandleHits() function is slightly different than mine. I believe that you’re missing sceneLoader.BrickDestroyed().

    void HandleHits ()
{
    timesHit++;
    int maxHits = hitSprites.Length + 1;

    if (timesHit >= maxHits)
    {
        breakableCount--;
        Debug.Log(breakableCount);
        sceneLoader.BrickDestroyed();
        PuffSmoke();
        Destroy(gameObject);
    }
    else
    {
        LoadSprites();
    }

NOTES:
(1.) I named my file SceneLoader instead of LevelManager, so you MAY need to change sceneLoader. I’m still pretty new, so not entirely sure
(2.) PuffSmoke() is just for animation that you can add later. Don’t worry about that!
(3.) Something that cause my levels not to load after clearing all bricks was I forgot to reset the brick count after dying on a previous run. (Doesn’t look like that’s the problem for you)

Hope that helps!


#3

Hey Phindah, thanks for the advice!

I gave it a try, but when I call levelManager.BrickDestroyed, it gives me an error (see below). It wants something to be changed with levelManager.BrickDestroyed but all the suggestions do nothing to remedy the issue.

I’ve also tried inserting the level loading code directly into the brick script but it still doesn’t do anything.


#4

Hmm! I dug threw some of my completed project… still new at this so forgive that it’s taking a little longer. Our level manager / scene manager files are similar. I also struggled to get it working, so here’s my whole Brick file for comparison (some things are slightly different):

Some things to check:

  1. I ended up using “using UnityEngine.SceneManagement;” at the very top AND “sceneLoader = GameObject.FindObjectOfType();” under Start()… that was able to get it working for me.
  2. Make sure you have the Level Manager game object on each scene. (Empty Game Object + Level Manager component).

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

public class Brick : MonoBehaviour {

public AudioClip crack; //For audio
public Sprite[] hitSprites;
public static int breakableCount = 0;
public GameObject smoke;

private int timesHit;
private SceneLoader sceneLoader;
private bool isBreakable;

// Use this for initialization
void Start () {
    isBreakable = (this.tag == "Breakable");
    if (isBreakable)
    {
        breakableCount++;
        Debug.Log(breakableCount);
    }
    sceneLoader = GameObject.FindObjectOfType<SceneLoader>();
    timesHit = 0;
}

void OnCollisionEnter2D(Collision2D collision)
{
    AudioSource.PlayClipAtPoint(crack, transform.position); //For audio
    bool isBreakable = (this.tag == "Breakable");
    if (isBreakable)
    {
        HandleHits();
    }
}

void HandleHits ()
{
    timesHit++;
    int maxHits = hitSprites.Length + 1;

    if (timesHit >= maxHits)
    {
        breakableCount--;
        Debug.Log(breakableCount);
        sceneLoader.BrickDestroyed();
        PuffSmoke(); //For animation
        Destroy(gameObject);
    }
    else
    {
        LoadSprites();
    }
}

void PuffSmoke ()
{
    GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject; // This makes an instance of Smoke where bricks are destroyed
    ParticleSystem smokePuffPS = smokePuff.GetComponent<ParticleSystem>();

    var smokeMain = smokePuffPS.main;
    smokeMain.startColor = gameObject.GetComponent<SpriteRenderer>().color;
}

void LoadSprites ()
{
    int spriteIndex = timesHit - 1;
    if (hitSprites[spriteIndex])
    {
        this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
    }
    else
    {
        Debug.Log("Brick Sprite Missing");
    }
}

}


#5

OMG you are amazing! Adding the private SceneLoader, then setting it equal in the Start did the trick. Thanks so much Phindah <3


#6

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.