This script (ShortRange tower bullet) acquires the monster component of another script (MonsterScript) and takes its health by the amount of damage this bullet does. The bullet is a public float.
The second line of the else method is getting an error (health - damage;)
void OnTriggerEnter(Collider co) {
if (co.gameObject.tag == "Monster") {
if (co.gameObject.GetComponent ().health <= 0) {
co.gameObject.GetComponent ().Death();
Destroy (co.gameObject);
}else{
co.gameObject.GetComponent ().myHealthBar.text = co.gameObject.GetComponent ().myHealthBar.text.Remove(co.gameObject.GetComponent ().myHealthBar.text.Length - 1 );
co.gameObject.GetComponent ().health - damage;
}
Here's the whole script
public class ShortRangeTowerBullet : MonoBehaviour {
// Speed
public float speed = 10;
// Target (set by Tower)
public Transform target;
//Damages the monster
public float damage;
void FixedUpdate() {
// Still has a Target?
if (target) {
// Fly towards the target
Vector3 dir = target.position - transform.position;
rigidbody.velocity = dir.normalized * speed;
} else {
// Otherwise destroy self
Destroy(gameObject);
}
}
void OnTriggerEnter(Collider co) {
if (co.gameObject.tag == "Monster") {
if (co.gameObject.GetComponent ().health <= 0) {
co.gameObject.GetComponent ().Death();
Destroy (co.gameObject);
}else{
co.gameObject.GetComponent ().myHealthBar.text = co.gameObject.GetComponent ().myHealthBar.text.Remove(co.gameObject.GetComponent ().myHealthBar.text.Length - 1 );
co.gameObject.GetComponent ().health - damage;
}
}
}
}
↧