Sprite not changing?

Edit: Turns out I wasnt calling the method to show the next sprite, not sure if that was in the instructions and I missed it but I found it in the lectures corrections?

Hi there,
I’ve followed Rick’s instructions to the T but the sprite won’t change when I hit it. I’m currently using Unity 2019.2.5f1 which is the latest version of Unity.


I was accidentally scrolled down a bit on the inspector, but I should note the tag is set to “Breakable” as I duplicated the original prefab.

I’ve included the entire Block.cs script so it can be parsed through to see if there is something I am missing

public class Block : MonoBehaviour {

//Config parameters

[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();
    }
    else
    {
        ShowNextHitSprite();
    }
}

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

private void OnCollisionEnter2D(Collision2D collision)
{
    if (tag == "Breakable")
    {
        HandleHit();
    }

}

private void HandleHit()
{
    timesHit++;
    if (timesHit >= maxHits)
    {
        DestroyBlock();
    }
}

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

}

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, 2f);
}

}

Yes, you probably missed it in the videos. Fortunely, you found the problem in the lecture project changes and were able to understand it. For this reason, there is nothing to worry about. Fixing a problem yourself is better anyway. :wink:

Next time, please feel free to post your solution in a new post and mark it as the solution. This helps other students find your solution more easily. I did that with my answer now but you may post your own answer and mark that instead if you want.

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

Privacy & Terms