How to destroy dead enemies

I tried using an OnCollisonEnter script to destroy enemy gameobjects once they’ve been shot, but they are destroyed instantly once they spawn and collide with the floor.

So can anyone tell me how I might destroy these gameobjects?

And also is there a way to destroy objects that fall off a level after a certain distance down? I’m thinking like a kill volume daemon in Realflow, any particles that fall off will be killed once they hit the volume, rather than falling forever.

I’m a total n00b so any advice at all is appreciated :]

For destroying the character after they are killed, the best way to do it, is in the life section, that signify that they have died is to put, “Destroy(gameObject,0.0)”.

That is assuming you have a health system worked out. The other way to do it, would be for you to make it so your projectile (or what ever is hurting the character) is tagged with a tag. For example, i will use ‘bullet’ as the tag:

function OnCollisionEnter() {
if (other.gameObject.tag == "bullet" ) {
Destroy(gameObject,0.0);
}

This simply says, if whatever is tagged ‘bullet’ hits the object, it will destroy itself. So simply attach this script to your enemy, or character to destroy them, and tag your weapon corresponding to the tag. You can also add tags, so you aren’t limited to the 8 or so default ones.

Now as for falling off the edge, the best way to do this, would to be make a cube, stretch it so it is bigger then your floor, delete the mesh render on it, and tag it: fallout, or what ever you want. Also, make sure you put it below your floor, so you can fall on top of it. Then attach this script to your character or who ever else you don’t want falling off.

function OnCollisionEnter() {
if (other.gameObject.tag == "fallout" ) {
Destroy(gameObject,0.0);
}

In case you didn’t notice, these are the exact same scripts, but they are so universal! You could also apply this to bullets, have it so when they have a collision they will destroy themselves instead of lying around for ever.

Good luck with your adventures! Also, just so you know, i have only be scripting/ using unity since less then a month ago, and i am decently proficient…though i do bug these guys a lot!

For the falling part try

function Update () {
	

	
	
	if(transform.position.y <=-4.6){
				
	Destroy(gameObject);
				
				
			
				
			}
	
}

now if they have health try

var enemyHealth = 50;

function Update(){

   if(enemyHealth <= 0){
destroy(gameObject);

I’m not sure on that last one but i think that is how it would be done.
Hope it helps dude

Awesome, thanks for the help. I haven’t yet figured out the health system, nor do I know where to look to figure it out :stuck_out_tongue:

It’s great to know it’s possible to pick it up so quickly, Unity really is well put together, I’m rapidly falling in love with it hehe

Ima try your second method here, so far I’ve gotten a “Unknown identifier: ‘other’” error, but I’m sure I’m just doing something wrong :stuck_out_tongue:

Thanks again :]

Thanks Crogin, I did actually try something like your first method but I guess I was writing it wrong, Ima try a few things and then search up on how enemyHealth works.

Thanks again :]

I’m still getting the Unknown Identifier:‘Other’ error, I’ve no idea what’s causing it tho, could it be becasue I’m using the Indie version maybe? :?:

Which script are you using?

Javascript

I mean which script from the ones posted above are you trying to use?

Oh, lol sorry…

The script xToxicInferno posted

function OnCollisionEnter() 

{
	if (other.gameObject.tag == "Finish" ) 
	{
		Destroy(gameObject,0.0);
	}
}

And this returns this error “Assets/Kill.js(4,13): BCE0005: Unknown identifier: ‘other’.”

Does you colliding object have the tag “Finish”?

Ah, thank you, this was my problem.

I was testing with a projectile object fired at a cube, I had the projectile tagged Finish and not the cube. I tagged it Finish becasue I couldn’t quite figure out how to add a tag :stuck_out_tongue:

It’s always silly little things you miss eh hehe, sorry about that, and thank you for your help :]

Actually I just realized that “other” was not being defined.

Try this:

function OnCollisionEnter(other : Collision) {

   if (other.gameObject.tag == "Finish" )
      Destroy(gameObject,0.0);
}

I tried this and it worked for me.

hth

Ohh, hmm, well this solved the Error, I’ve been wondering the past 2 days what the (collision : Collision) part of the OnCollisionEnter command was :stuck_out_tongue:

It seems now tho when I check the Is Trigger box for the finish object (the projectile) it passes straight through the object the script is on (the cube).

Sorry, I just edited my post there. I was hoping that I got it in time. I was using the onTriggerEnter in one of my games so I just copied that over. You can use the onCollisionEnter call too though (as above). The important thing here was to define the “other” variable. I hope that makes sense.

Ok, thank you very much, it’s working fine now :]

One last question though, would it be more appropriate for me to use the OnTriggerEnter command? I chose to use OnCollisionEnter becasue I wasn’t sure if I could figure out how Triggers work :stuck_out_tongue:

The scripting reference explains that the Collision class contains more information like contact points, impact velocity, etc. So since that stuff is not needed in this example, you could use the onTriggerEnter. I don’t know how much it really matters in this case though.

file:///C:/Program%20Files/Unity/Editor/Data/Documentation/Documentation/ScriptReference/Collider.OnCollisionEnter.html