The block collapses a second or two after hitting the ball with it

did everything exactly as in the course. but the result was a little different. when the ball hits the block, the ball bounces off, and the block collapses only a second or two after it hits. the ball may even hit the block several times before it disappears from the scene. How to fix it?

Hi,

How many hits have you set the block to have?

Also, when you say collapses, what do you mean?

1 hit for each block. when the ball hits the block, the block should disappear immediately. But the block does not disappear for some time, and the ball can strike it again. I need to fix it so that the block disappears immediately after hitting the ball

What does your code for the block look like, can you copy/paste it into a reply here and apply code formatting please.


See also;

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

public class Block : MonoBehaviour
{
    // config params
    [SerializeField] AudioClip breakSound;
    [SerializeField] GameObject blockSparklesVFX;
    [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.CountBlock();
        }
    }

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

    private void HandleHit()
    {
        timesHit++;
        int maxHits = hitSprites.Length + 1;
        if (timesHit >= maxHits)
        {
            DestroyBlock();
        }
        else
        {
            ShowNextHitSprite();
        }
    }

    private void ShowNextHitSprite()
    {
        int spriteIndex = timesHit - 1;
        if (hitSprites[spriteIndex] != null)
        { 
        GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
        }
        else
        {
            Debug.LogError("Block sprite is missing from array" + gameObject.name);
        }
    }

    private void DestroyBlock()
    {
        PlayBlockDestroySFX();
        Destroy(gameObject, 1f);
        level.BlockDestroyed();
        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);
    }
}

Hi,

In your DestroyBlock method you have stated that you do not want the GameObject to be destroyed for 1 second;

Destroy(gameObject, 1f);

Change this to;

Destroy(gameObject);

You’ll probably find that the delay goes away now. Let me know.

Hope this helps :slight_smile:

Thank you very much. problem solved :slightly_smiling_face:

1 Like

You’re very welcome :slight_smile:


See also;

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

Privacy & Terms