trying to blow object up with tag

Hi guys I was wondering if you can correct me where I am going wrong I am firing my bullet and when it hits the enemy it is not exploding and destroying the Enemy1 object that has been tagged, this is the code I have attached to my bullet prefab:

**** attached to the bullet prefab ****

var lifetime = 1.0;
var explosion : Transform; // create the explosion in the inspector

function OnTriggerEnter(collide : Collider)
{
// Check for enemies via a tag
if(collide.gameObject.tag == “Enemy1”)
{

// create the explosion on impact

if(explosion)
{
Instantiate(explosion, transform.position, transform.rotation);
}

Destroy(gameObject, lifetime);
}
}

any help would be great :slight_smile:

both objects have rigidbodys aswell.

I tried adding the Activate trigger function from scripts and made the target Enemy1 and the source bullet but it did not work, I then ticked the trigger option on the bullet and it has the explosion prefab attached but it does not work, I don’t understand why it will not blow up I must be missing something here

Try adding just under the OnTriggeredEnter(), so you are sure its your script, not something else.

Debug.Log(“triggered”);
so you know if the collider is triggered at all. If not then you misconfigured it, which you probably did as the script looks fine to me.

hi adnan I done the debug.log, there are no errors I am very confused very strange I cannot work it out

Well… Do you have a particle effect that is supposed to be the explosion? If so, then you have your explosion set to type transform… You should make a particle effect, and put it into a prefab. Next, all you have to do is just redefine explosion as a prefab:

var explosion : GameObject;

then, to make the enemy destroy:

Destroy(collide.gameObject);

Also, you are using explosion as a bool in your second if statement… I don’t think that’s going to return anything for you.

hi Epictickle.

Ok let me see if this is correct I have the following

  1. Destroy(gameObject, lifetime); which destroys my bullet can I add another destroy just underneath to destroy the explosion
    Destroy(collide.gameobject); or Destroy(explosion.gameobject);

  2. also I haven’t set a Boolean value in my if statement should I define a variable like this
    exploded bool = false
    Can you maybe just write a snippet of code so I can try and understand it as this will then see where I am going wrong

var lifetime = 1.0;
var explosion : Transform; // create the explosion in the inspector

function OnTriggerEnter(collide : Collider)
{
// Check for enemies via a tag
if(collide.gameObject.tag == “Enemy1”)

{

// create the explosion on impact

if(explosion)
{
Instantiate(explosion, transform.position, transform.rotation);

}

Destroy(gameObject, lifetime);
}
}

Ok I added this and it has now removed the enemy2, thank you

Destroy(collide.gameObject);

so if I wanted to hit the object twice with my bullet what would I need to add

is this correct if wanted to do two objects as it is not working

// Check for enemies via a tag
if(collide.gameObject.tag == “Enemy1” collide.gameObject.tag == “Enemy3”)

It’s because the bullet isn’t hitting the enemies at the same time… Either change the “” to a “||”, or create another if statement.

Why do you need separate tags for your enemies, though?

Why not just tag all enemies as “Enemy” and leave it at that?

Hi Epictickle I changed my other enemy to enemy1 and tagged it and it worked thank you for that, no all I need is to keep track of when I have killed all enemies I have the amount I have killed and then it loads to another scene.