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!