0
\$\begingroup\$

I'm trying to make an enemy for my game, and everything works, except for the bit of code where the enemy is supposed to die.

My player attacks but nothing happens.

I filled in the prefabs, I have the right tags, I tried remaking it and I can't find anything that is wrong.


What am I doing wrong?

#pragma strict
var EnemyItself : GameObject;
var EnemyCorpse : GameObject;

function OnTriggerEnter(other : Collider) {
    if (other.gameObject.tag == "Hero Punch") {
        EnemyItself.SetActive(false);
        EnemyCorpse.SetActive(true);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Please, indicate what "does not work". Is the collision detected ? Is the condition called ? Add some Debug.Log to determine where does the error come from. \$\endgroup\$ Commented Jan 2, 2017 at 22:47
  • \$\begingroup\$ Yeah sorry about that. I updated it. \$\endgroup\$ Commented Jan 3, 2017 at 1:26

1 Answer 1

1
\$\begingroup\$

As previously pointed out, through comments, there is nothing wrong with your code. Assuming you have referenced the game objects and are using tags correctly, the only thing left appears to be your rigidbody.

This also seems to be the confirmed issue, in the comments.


Collision events require at least one of the game objects to contain a RigidBody component. Ideally, these would be on the moving objects, and we would not have a situation where two non-moving objects enter a collision event.

It is also worth mentioning that you need to use RigidBody for 3D collion events, where 2D collision events will require a RigidBody2D.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.