x


How do i kill an "enemy with character controller on + few child objects on"

Helor I'm trying to seek help with this question as previously i tried on various codes on destroying game objects once i collide with them, it works but not for my AI character with character controller and few childs on it. I wanted to test if the collision works for 1 hit kill before i add in GUI health textures for it.

But it seems like this script that i'm going to attach works on a small cube with box collider and with "is trigger" being ticked, but not towards my enemy. Hope someone can tell me another different way of coding it? or do i just need to tweak something else out of it?

I'm still new to Unity between and am eager to learn more from other people as well :) Thanks heaps!

Here's my code:

//var score = 0;
var explosionPrefab : GameObject;
//var explosionLife : float = 10;
var Collider = CharacterController;


function OnTriggerEnter( other : Collider ) 
{
    if (other.tag == "Player") 
    {
        //score += 5;
        print ("ENEMY BLOWN UP");
        var expPos = other.transform.position;
        var exp : GameObject = Instantiate (explosionPrefab, expPos, Quaternion.identity);
        Destroy (other.gameObject);
    }
}
more ▼

asked May 18 '10 at 07:20 AM

jason gravatar image

jason
17 2 2 4

Could you specify what "is not working" means? Does it print the message? Are there leftover GameObjects? Is anything happening at all?

May 18 '10 at 07:29 AM StephanK

sorry forgot to mention that. My enemy is not destroyed. yet it is still running around the area. Print message was not shown either.

what the enemy does is just continue attacking me.

May 18 '10 at 08:20 AM jason

i think you are trying to destroy the player. and you cant just destroy the player for data loss reasons. are there any errors?

Apr 02 at 10:21 AM BeB_Wir3

Do you even have a trigger attached to the enemy? OnTriggerEnter can't be called without a trigger. It's obvious, but sometimes people forget :)

Apr 07 at 10:50 AM AutoFredrik
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Hard to tell since it could be many things. Here's a list of what I think might be worth checking:

  • Have you double-checked the tag name, including upper/lowercase?
  • Is the enemy's collider on the same game object as your script (not on a parent of child)?
  • Is the Is Trigger property of the enemy's collider ticked?
  • Are both objects on Layers that are set to collide with each other in Unity's physics settings?

Also, I think @BeB_Wir3 is quite right, once you get it to work, it will try to destroy your player, not the enemy. That last line should be GameObject.Destroy(gameObject); (or this.gameObject if you want to be more explicit about were the property comes from).

more ▼

answered Apr 07 at 10:58 AM

Cygon4 gravatar image

Cygon4
306 2 4 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2485
x764
x420
x409
x60

asked: May 18 '10 at 07:20 AM

Seen: 1362 times

Last Updated: Apr 07 at 10:58 AM