Damaging an enemy

Hey guys!

Im creating a Tower Defence game and I can’t get the bullet to acess the script on the enemy and start taking health off.

This is the Health Script

using UnityEngine;
using System.Collections;

public class Health : MonoBehaviour
{
    public float health = 100f;

    void Update()
    {
        if (health <= 0) {
            Destroy (gameObject);
        }
    }
}

and this is the Bullet Script
using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour
{

public float speed = 10;
public Transform target;

public float DPS = 30;

void FixedUpdate() {
if (target) {
Vector3 dir = target.position - transform.position;
GetComponent ().velocity = dir.normalized * speed;
} else {
Destroy (gameObject);
}
}

void OnTriggerEnter(Collider other) {
if (other.tag == “Enemy”)
{
gameObject.GetComponent().health -= DPS * Time.deltaTime;
Destroy (gameObject);
}
}
}

I think it is something to do with the OnTriggerEnter area of the code but I’m not too sure! all help will be appreciated. Thanks!

Yeah basically the gameObject in bullet is the bullet’s gameobject, not the enemies… So you’re doing the right thing, just to the wrong object lol.

You’d want to change it to target the enemy, like so:

public class Bullet : MonoBehaviour
{

public float speed = 10;
public Transform target;

public float DPS = 30;


void FixedUpdate() {
if (target) {
Vector3 dir = target.position - transform.position;
GetComponent<Rigidbody> ().velocity = dir.normalized * speed;
} else {
Destroy (gameObject);
}
}

void OnTriggerEnter(Collider other) {
if (other.tag == "Enemy")
{
Health health = null;
if(health  = other.GetComponent<Health>())
health.health -= DPS * Time.deltaTime;
Destroy (gameObject);
}
}
}
1 Like

Thank you,
Im also having an issue in which it is only shooting one bullet at each enemy, not at the intervals that I set.
using UnityEngine;
using System.Collections;

public class Tower : MonoBehaviour
{
public GameObject bulletPrefab;
public float rotationSpeed =35;
public float interval = 1;

void start() {
InvokeRepeating (“OnTriggerEnter”, interval, interval);
}

void OnTriggerEnter(Collider co){
if (co.tag == “Enemy”) {
GameObject g = (GameObject)Instantiate(bulletPrefab, transform.position, Quaternion.identity);
g.GetComponent().target = co.transform;
}
}

void Update() {
transform.Rotate (Vector3.up * Time.deltaTime * rotationSpeed, Space.World);
}
}

It’s because you can’t use Invoke for methods with parameters (OnTriggerEnter has the Collision parameter). To be honest OnTriggerEnter is a method normally saved only for when objects collide with each other, and is called automatically by the physics engine in Unity. Your best bet would be to make your own method and call that one instead, which has no parameters:

void Start()
            {
                InvokeRepeating("SomeMethodName", 1, 1);
            }

            void SomeMethodName()
            {
                GameObject g = (GameObject)Instantiate(bulletPrefab, transform.position, Quaternion.identity);
                g.GetComponent<Bullet>().target = co.transform;
            }