[Solved] Bricks stopped breaking

After updating my Brick.cs code to detect a win condition my bricks stopped working. Any Idea what would cause this. Everything was working fine up until the addition of the code in this section.

This is my Brick Script.


public class Brick : MonoBehaviour
{
    public Sprite[] hitSprites;
    private int timesHit;
    private LevelManager levelManager;

    public static int breakableCount = 0;
     bool isBreakable;

    // Use this for initialization
    void Start ()
    {
        
        isBreakable = (this.tag == "Breakable");
        if (isBreakable)
        {
            breakableCount++;
        }

        timesHit = 0; 
        levelManager = GameObject.FindObjectOfType<LevelManager>();
    }

   

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (isBreakable)
        {
            HandleHits();
        }
    }

    void HandleHits()
    {
        timesHit++;
        int maxHits = hitSprites.Length + 1;
        if (timesHit >= maxHits)
        {
            breakableCount--;
            
            Destroy(gameObject);
        }

        else
        {
            Loadsprites();
        }

    }

    void Loadsprites()
    {
        int spriteIndex = timesHit - 1;
        if (hitSprites[spriteIndex])
        {
            this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
        }
    }


    void SimulateWin()
    {
        levelManager.LoadNextLevel();
    }
}

Any suggestions would be appreciated. I really like what I’m working on here!

Also Running on Unity5 and VS. And will saving to OneDrive affect anything as far as loading times for assets.


SOLVED = Missed line and Typo.

Hi,

Check he tags on the bricks in the scene, and on your prefabs.

Regarding Unity 5, there will be some differences between the course materials and your version of Unity. Primarily how the levels are loaded.

Application.LoadLevel is obsolete and has been replaced with SceneManager.LoadScene. This requires the following directive at the top of your script;

using UnityEngine.SceneManagement;

Also, how the particle system is access has also changed a little.

You will find answers to these issues on the forum as it has been covered a lot already. Additionally the GaneDev.tv team have added additional resources and lectures to explain the differences.

Regarding OneDrive, if this is just a local drive then you should be OK, unless it periodically synchronises, if so you may get some issues if there is a nun fight over file access.

I would recommend just keeping your development working directory in something like C:\Dev\ or better still, on a separate drive.


See also;

Thank you. I got it working. I missed a line of code and had a capital typo. I have seen App.loadlevel is obsolete but it seems to still work. I will change it for the sake of learning. Thanks again

1 Like

You’re very welcome. :slight_smile:

It will depend on your version a little. Initially it was just depreciated, obsolete from 5.3 I believe.

5.6. Now I’m trying to figure out how to fix load next level under my new SceneManager changes

You can access the scene’s buildIndex and then increment this value, for use in the LevelManager’s LoadNextLevel method.


See also;

Privacy & Terms