Sprite not changing

my sprite is not changing can’t figure out what the problem is. Even assigned it to maxsprite.

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

public class Block : MonoBehaviour
{
    //config param
    [SerializeField] AudioClip destroySound;
    [SerializeField] GameObject blocksparlesVFX;
    [SerializeField] int Maxhit;
    [SerializeField] Sprite[] hitSprites;

    //Cached reference
    Level level;
    GameStatus gameStatus;

    //state variable
    [SerializeField] int timesHit; //Only for debug purposes 

    private void Start()
    {
        countbreakableblocks();
        gameStatus = FindObjectOfType<GameStatus>();
    }

    private void countbreakableblocks()
    {
        level = FindObjectOfType<Level>();
        if (tag == "Breakable")
        {
            level.CountBlocks();
        }
    }

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

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

    private void HandleHit()
    {
        timesHit++;
        if (timesHit >= Maxhit)
        {
            blockdestroy();
        }
    }

    private void blockdestroy()
    {
        AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
        
            level.destroyedblock();
            gameStatus.addtoscore();
            Destroy(gameObject);
            TriggersparklesVFX();
        
    }

    private void TriggersparklesVFX()
    {
        GameObject sparkles = Instantiate(blocksparlesVFX, transform.position,transform.rotation);
        Destroy(sparkles, 2f);
    }
}

I think the problem is in your collision it says if it’s breakable handle the hit, if it isn’t breakable handle the sprite, I think you should try moving handle sprite in with your is breakable tag in the collision, hope that helps!

1 Like

thank you so much. I was literally thinking of starting all over again from this project.

1 Like

everyone having same problem I would recommend to check your code from his code in github.

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

Privacy & Terms