Bricks not updating sprites, but they do break after 1/2/3 hits

For some reason, my sprites aren’t updating when hit by the ball. They are destrying as per the number of hits in the serialized field but the sprites just are not updating.

public class Block : MonoBehaviour
{
//config params
[SerializeField] AudioClip breakSound;
[SerializeField] GameObject blockSparklesVFX;
[SerializeField] int maxHits;
[SerializeField] Sprite hitSprites;

//cached reference
Level level;

//state variables
[SerializeField] int timesHit;  //TODO only serialized for debug purposes

private void Start()
{
    CountBreakableBlocks();

}

private void CountBreakableBlocks()
{
    level = FindObjectOfType<Level>();

    if (tag == "Breakable")
    {
        level.CountBlocks();
    }
}

private void OnCollisionEnter2D(Collision2D collision)
{
    HandleHit();
}

private void HandleHit()
{
    if (tag == "Breakable")
    {
        timesHit++;
        if (timesHit >= maxHits)
        {
            DestroyBlock();
        }
    }else
    {
        ShowNextHitSprite();
    }
}



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


private void DestroyBlock()
{
    PlayBlockDestroySFX();
    level.BlockDestroyed();
    Destroy(gameObject);
    TriggerSparklesVFX();
}

private void PlayBlockDestroySFX()
{
    FindObjectOfType<GameSession>().AddToScore();
    AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);
}

private void TriggerSparklesVFX()
{
    GameObject sparkles = Instantiate(blockSparklesVFX, transform.position, transform.rotation);
    Destroy(sparkles, 1f);
}

}

56%20PM 09%20PM

26%20PM

Hi Josh,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

Will do! Makes lots of sense :slight_smile:

How are you getting on with this?

I just moved on… I couldn’t find the problem and it really wasn’t a big deal. I understood the concept he was going for and from what my research online tells me, Unity updates itself all the time and I just chalked it up to some different approach that needs to be taken in the future.

Thank you for checking in!

My last post was a complete load of horse hockey…I figured it out just now

I just looked at my code and found that my if/else statement under my HandleHit() function was not written correctly. The else statement needed to be nested inside the second if statement and I wrote it connected to the first if statement, so it never had an opportunity to run.

Thanks for keeping me on the right path! :slight_smile:

Good job on fixing the issue yourself. :slight_smile:


See also:

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

Privacy & Terms