Hi, I want to be able to link the script of my projectiles to the enemy hands, so that they can damage them, but after you destroy one hand, you get a null reference exception, so i'm wondering how you do it without getting that, or even a work around, thanks.
projectile script:
public class ProjectileController : MonoBehaviour {
private Rigidbody2D rb2d;
private Boss boss;
private Hands hand1;
private Hands hand2;
public int damage;
public float speed;
// Use this for initialization
void Start () {
rb2d = GetComponent ();
boss = GameObject.FindGameObjectWithTag ("Boss").GetComponent();
hand1 = GameObject.FindGameObjectWithTag ("Hand1").GetComponent();
hand2 = GameObject.FindGameObjectWithTag ("Hand2").GetComponent ();
}
// Update is called once per frame
void Update () {
rb2d.velocity = new Vector2 (speed, rb2d.velocity.y);
}
void OnTriggerEnter2D(Collider2D col){
if (col.CompareTag ("Hand1")) {
hand1.Damage (damage);
boss.Damage (damage);
Destroy (this.gameObject);
}
if (col.CompareTag ("Hand2")) {
hand2.Damage (damage);
boss.Damage (damage);
Destroy (this.gameObject);
}
if (col.CompareTag ("Boss")) {
boss.Damage (damage);
Destroy (this.gameObject);
}
}
}
boss script:
public class Boss : MonoBehaviour {
private Player player;
private Rigidbody2D rb2d;
private Animator anim;
public float maxHealth;
public float currHealth;
public float handsHealth;
public float rageLimit;
public float moveSpeed;
// Use this for initialization
void Start () {
//player = GameObject.FindGameObjectWithTag ("Player");
player = GameObject.FindGameObjectWithTag ("Player").GetComponent();
anim = gameObject.GetComponent ();
currHealth = maxHealth;
anim.SetBool ("Angry", false);
}
// Update is called once per frame
void Update () {
//movement
//transform.Translate (new Vector3 (0, moveSpeed, 0) * Time.deltaTime);
if (currHealth <= rageLimit) {
Angry ();
}
if (currHealth <= 0) {
Die ();
}
}
// IEnumerator FireHands(){
// return yield 0;
// }
//
// IEnumerator FireProjectiles(){
// return yield 0;
// }
public void Damage(int dmg){
currHealth -= dmg;
}
void Die(){
Destroy (gameObject);
}
void OnCollisionEnter2D(Collision2D col){
if (col.collider.CompareTag("Bound")){
moveSpeed *= -1;
}
}
void Angry(){
anim.SetBool ("Angry", true);
transform.Translate (new Vector3 (0, moveSpeed, 0) * Time.deltaTime);
}
}
↧