I have created a script for a “bomb” ability that is supposed to destroy all the coins (they get continously spawned in a random position) in an area but for some unknown some of the coins dont get deleted even on multiple tries. I had the coin prefab in a layer that i put in a layermask for the overlap circle, then to try i removed the layermask and it destroyed other gameobjects just fine but some coins continued not getting destroyed, i also added a condition in wich if the colliders have the coin tag the object with that collider gets destroyed but it still doesnt work.
This is the script for it
//bomb variables
[SerializeField] private GameObject bombCenter;
[SerializeField] private LayerMask layerMask;
Vector2 point;
public void Bomb()
{
Collider2D[] coins = Physics2D.OverlapCircleAll(point, 15);
foreach (Collider2D col in coins)
{
if (col.tag == "Coin")
{
Destroy(col.gameObject);
scoreInt += 1;
}
}
}
This is the full script if you need it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
//movement control variables
public Rigidbody2D rb;
public static float speed = 5f; // increases with the score
Vector2 movement;
//dash control variables
private bool dashing = false;
private float dashTime = 0.15f;
public static float dashCD = 3f;
private float nextDashTime = 0f;
public TrailRenderer trail;
private float dashSpeed = 20f; // this changes the distance traveled in dashTime
public AudioSource dashSound;
private bool canDashSound = false;
public SpriteRenderer playerSR;
//bomb variables
[SerializeField] private GameObject bombCenter;
[SerializeField] private LayerMask layerMask;
Vector2 point;
//death variables
public ParticleSystem explosion;
private bool canExplode = true;
public Transform playerTransform;
public static int tolerance;
//sound control variables
public AudioSource collectCoin;
public AudioSource BGM;
public AudioSource deathSound;
// score control variables
public int scoreInt;
public Text score;
// scene control variables
public static bool canLoad = false;
private void OnTriggerEnter2D(Collider2D collect)
{
collectCoin.Play();
scoreInt = scoreInt + 1;
score.text = scoreInt.ToString();
}
void Start()
{
BGM.mute = false;
point.x = bombCenter.transform.position.x;
point.y = bombCenter.transform.position.y;
}
void Update()
{
if (Upgrades.nTolerance == 0)
{
tolerance = 20;
}
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (Counter.count >= tolerance)
{
StartCoroutine(Death());
}
if (dashing)
{
trail.emitting = true;
}
else
{
trail.emitting = false;
}
if (Input.GetMouseButtonDown(0))
{
Bomb();
}
// dash ability
if (Input.GetKeyDown(KeyCode.Space) && Time.time > nextDashTime)
{
nextDashTime = Time.time + dashCD;
StartCoroutine(Dash());
}
if (Time.time > nextDashTime && canDashSound)
{
StartCoroutine(DashCDAnim());
}
}
public void Bomb()
{
Collider2D[] coins = Physics2D.OverlapCircleAll(point, 15);
foreach (Collider2D col in coins)
{
if (col.tag == "Coin")
{
Destroy(col.gameObject);
scoreInt += 1;
}
}
}
IEnumerator DashCDAnim()
{
dashSound.Play();
canDashSound = false;
playerSR.color = Color.cyan;
yield return new WaitForSeconds(0.2f);
playerSR.color = Color.white;
}
IEnumerator Dash()
{
canDashSound = true;
dashing = true;
rb.velocity = movement * dashSpeed;
yield return new WaitForSeconds(dashTime);
rb.velocity = movement * speed;
dashing = false;
}
IEnumerator Death()
{
if (canExplode)
{
BGM.mute = true;
deathSound.Play();
canExplode = false;
playerTransform.DetachChildren();
Destroy(this.gameObject);
TotalCoins.coins += scoreInt;
explosion.Play();
canLoad = true;
}
yield return new WaitForSeconds(1.5f);
}
void FixedUpdate()
{
if (!dashing)
{
rb.velocity = movement * speed;
}
}
}