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);
}
}