Trying to Create Bomb Power Up - Almost there?

Hi

I’m trying to create a power up designed to explode destroying everything in the blast area. Using Physics.Overlapshere I am attempting to create an array of all the colliders in the blast zone. Then I’m trying to access the objects assigned to that collider and then to access the objects attached script ‘DestroyByContact’ which I then hope to call the public function ‘Destroyed’.

I don’t think I’m far off finishing but I think I need a bit of help with accessing objects on the colliders thing.

I think I have included everything that I’m working with here. Shout out if not. :slight_smile:
Any help would be great please.
Cheers :slight_smile:

My code

Bomb script (as you can probably see, I get lost once I get into the code following ‘if (col.collider.tag’ ) :confused:

public class Bomb : MonoBehaviour 
{
	
	public Transform explosionPosition = transform.position;
	public float explosionRadius = 10.0;
	public Collider[] colliders = Physics.OverlapSphere(explosionPosition, explosionRadius);
	
	private GameObject victim;
	

	void OnTriggerEnter(Collider other)
	{
		if (other.tag == "Player") 
		{
			Destroy(gameObject);

			Explode();
		}
		else
		{
			return;
		}
	}


	void Explode()
	{
		
		foreach (Collider col in colliders)    
		{
			if (col.collider.tag == "LargeHazard" | col.collider.tag == "MediumHazard" | col.collider.tag == "Hazard")
			{
				//Destroy(col.collider.gameObject);
				GameObject victimObject = Collider.gameObject;
				victim = victimObject.GetComponent<DestroyByContact>();

				victim.Destroyed();
			}
		}
	}
}

DestroyByContact (I’ve only included the function I want to call)

public void Destroyed()
	{
		gameController.AddScore(scoreValue);
	}

There’s a couple of problems here:

1: You declare the colliders array as a field, meaning that this code:

public Collider[] colliders = Physics.OverlapSphere(explosionPosition, explosionRadius);

Will be called once, when the object is generated. You need to put that line inside the explode method:

void Explode()
{
    Collider[] colliders = Physics.OverlapSphere(explosionPosition, explosionRadius);
    foreach (Collider col in colliders)    
        { ...

2: This can’t possibly compile:

GameObject victimObject = Collider.gameObject;
victim = victimObject.GetComponent<DestroyByContact>();
 
victim.Destroyed();

Victim is declared as a GameObject further up, so the Destroyed-method won’t be available, as it’s not a GameObject-method. Victim needs to be declared as a DestroyByContact, and you should only have it available within the Explode method. Remove “private GameObject victim” from your fields, and declare it locally in the Explode method as a DestroyByContact like this:

GameObject victimObject = Collider.gameObject;
DestroyByContact victim = victimObject.GetComponent<DestroyByContact>();
 
victim.Destroyed();

That should make your code work!

3: As an additional protip; generally I’d recommend against using tags, and instead checking for what scripts are on the object. Tags are generally a mess, and if you mess up and forget to put one in, stuff won’t work for no particularly clear reason. After your project has grown a bit, changing the names of tags will also become near impossible, as there’s no easy way to check what scripts are checking for what tags. In my opinion, a better way to do your Explode method would be:

foreach (Collider col in colliders)    
{
    GameObject victimObject = Collider.gameObject;
    DestroyByContact victim = victimObject.GetComponent<DestroyByContact>();

    //Check if the DestroyByContact script actually exists
    if(victim != null)
        victim.Destroyed();        
}