[SOLVED]Block Breaker: Problems adding Powerups

Hi there!
After a couple weeks hiatus I’ve thrown myself back into the Block Breaker project with enthusiasm! Problem is I seem to hit a bit of a wall and cant figure out why my code is behaving as such…

I’ve added a few powerups including a bomb which appears at the position the ball is in after 5 seconds and destroys any blocks in its collider using this code.

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

public class Explosion : MonoBehaviour
{
    GameSession score;
    Block block;
    public void Start()
    {
        
    }

    private void DestroySelf()
    {
        DestroySelf();
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {

        
        
        if (collision.gameObject.tag == "Breakable")
        {
           Debug.Log("Detected");
            block = FindObjectOfType<Block>();            
            block.Destroyblock();
                    
            

        }
        
        
        
    }
}

It’s calling the Destroyblock() method which destroys the block, and adds the points but the problem is it’s destroying a different block entirely!
That Block behaves as expected, adding points, instantiating VFX and destroying itself but I cant get this code to only call the blocks it is touching.

Here is the method that is being called…

 public void Destroyblock()
    {
        AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);
        score.AddToScore();
        Destroy(gameObject); //refering to the game object this script is linked to as a component.
        level.minusBreakableBlock();
        TriggerParticlesVFX();
        CalculateDrop();
        ChooseDrop();
        HandlePowerUp();
    }

I’m hoping it’s an easy fix, and thank you in advance!

P.S. If anyone was interested about the powerups I’ve added for their own project I can share the code with them!

Hi,

Does the Destroyblock method get called?

Hey Nina,

The Method is called but it’s not calling on the block which is colliding with the explosion. Instead it’s calling other random blocks.

Thanks for the response!

On other random blocks sounds like a reference issue. And here is the culprit:
block = FindObjectOfType<Block>();

FindObjectOfType returns the first object of type Block, whatever Unity defines as “first”.

What you actually meant is:
block = collision.gameObject.GetComponent<Block>();

You want a specific Block component, and you already know a component attached to the block. The data is stored in collision.

2 Likes

Having searched 2 evenings for a solution this response couldn’t have made me happier!

It works perfectly! I had a feeling it was just finding the first object it could find; but I couldn’t find a way to call a method from a specific object in a collision.

Thank you so much!

:smile:

You are welcome. :slight_smile:

Referencing issues are one of the most common issues in programming. They happen to the best. Your explanation of what had been happening was very helpful. Once I read about “random blocks”, I knew that I have to look for a FindObjectOfType method.

2 Likes

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

Privacy & Terms