I have an enemy ship and a player which is sopposed to shoot a projectile at the enemy and then the enemy should pull a value from an array from a script attaced to a controller.
public GameObject gameController;
void OnTriggerEnter (Collider other) {
if (other.tag == "projectile1") {
health -= gameObject.GetComponent<Controller>().damage[0];
}
}
And I have a controller scrtipt.
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
public float[] damage;
void Update () {
}
}
I have tested this with hard coded values and it works just fine.
Just incase I somehow screwd up somewhere else in the script here is the full enemy script.(Some of the lines seem to be getting cut off in my preveiw so I there might be some brackets missing etc.)
using UnityEngine;
using System.Collections;
public class BasicAI : MonoBehaviour {
public GameObject gameController;
public GameObject target;
public float speed;
public float stopDistance;
public float attackDistance;
public float weaponCoolDown;
public Transform Barrel_L;
public Transform Barrel_R;
public GameObject projectile;
public float health;
public GameObject expolsionParticle;
float weapomTimer;
void Update () {
transform.LookAt (target.transform);
if (Vector3.Distance(target.transform.position, transform.position) > stopDistance) {
rigidbody.AddRelativeForce (0, 0, speed);
}
else {
rigidbody.AddRelativeForce (0, 0, -speed);
}
weapomTimer += 1 * Time.deltaTime;
if (Vector3.Distance(target.transform.position, transform.position) < attackDistance) {
Debug.Log("ready to fire");
if (weaponCoolDown < weapomTimer) {
Instantiate (projectile, Barrel_L.position, Barrel_L.rotation);
Instantiate (projectile, Barrel_R.position, Barrel_R.rotation);
weapomTimer = 0;
Debug.Log("fire");
}
}
if (health < 0) {
Instantiate (expolsionParticle, transform.position, transform.rotation);
Destroy (gameObject);
}
}
void OnTriggerEnter (Collider other) {
if (other.tag == "projectile1") {
health -= gameObject.GetComponent<Controller>().damage[0];
}
}
}