I need help with this
I made a script that has to kill the enemy inside my bullet as component but it wont work it wont print health or Destroy it but i made the same script another time and i did work can someone explain why it wont work.
Script: using UnityEngine; using System.Collections;
public class enemyKiller : MonoBehaviour {
public int enemyHealth;
public int bulletDamage;
// Use this for initialization
void Start ()
{
enemyHealth = 100;
bulletDamage = 1;
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnemy(Collision col)
{
if (col.gameObject.name == "enemy")
print(enemyHealth);
if (col.gameObject.name == "enemy")
enemyHealth = enemyHealth - bulletDamage;
if (enemyHealth == 0)
if (col.gameObject.name == "enemy")
Destroy(col.gameObject);
}
}
Are you sure its colliding? Check your colliders and tags and see that it actually collides at first. I would recommend to put the 3rd if statement in Update() so it will keep track of the enemy health through every frame, but then you need it to be
Answer by JedBeryll · May 29, 2016 at 01:14 PM
What is OnCollisionEnemy? If you don't call it yourself somewhere it's never going to run. Normally it would be OnCollisionEnter.
Follow this Question
Related Questions
How to hide a sprite on top of another sprite when the lower one comes into contact with the player? 0 Answers
Update() and FixedUpdate() not running on C# scripts 0 Answers
Understanding how scripts are used by gameobjects 1 Answer
How to Start/Stop Scripted Animation using a Trigger? 1 Answer
The lifespan of an object instance in a Monobehavior script 0 Answers