It’s not clear (at least, to me) what you’re trying to do here. That said, the error is very clear. Apparently, your “myTarget” variable references a Unity Transform object, which indeed does not have a “health” member - just as the error states. I’d suggest that you 1) try to better explain the problem and 2) post the entire method in question rather than just a small snippet.
This is the shooting section of code that gets called when a target is in range/ in my List.
IEnumerator shootProjectile() {
int index = 0;
for(int i = 0; i < shotPositions.Count; i++) {
audio.PlayOneShot(boomNoise);
Transform newMissile = PoolManager.Pools["EBullets"].Spawn(tankShell.transform, shotPositions[index].position, shotPositions[index].rotation);
PoolManager.Pools["Effects"].Spawn(bangFX.transform, shotPositions[index].position, shotPositions[index].rotation);
newMissile.GetComponent<RocketMissileEnemyCS>().myTarget = foundTarget;
index++;
yield return new WaitForSeconds(0.2f);
}
}
This is the code on the homing missile, which follows the target up after target is destroyed which I don’t want.
Target moves up when its dead to trigger ontriggerexit, so target is removed from My Target List, but yeah any missiles still out there will go straight up… still following the dead target.
void Update() {
transform.Translate(Vector3.forward * Time.deltaTime * mySpeed);
myDist += Time.deltaTime * mySpeed;
if(!despawned myDist >= myRange){
PoolManager.Pools["EBullets"].Despawn(this.transform);
despawned = true;
}
if(myTarget.health > 0){
transform.LookAt(myTarget);
}
}
void OnTriggerEnter(Collider other){
if(other.gameObject.tag == "Air Good" || other.gameObject.tag == "Ground Good" || other.gameObject.tag == "Base Good" || other.gameObject.tag == "Ground"){
other.gameObject.SendMessage("TakeDamage", myDamageAmount, SendMessageOptions.DontRequireReceiver);
Explode();
}
}
void Explode(){
if(!despawned){
Collider[] colliders = Physics.OverlapSphere (transform.position, explosionRadius); //Sets a collider sphere publiciable.
//For each new thing the hit public, that we define in the void, that the explosionRadius collides with.
foreach (Collider hit in colliders) {
//If the thing we hit is a rigidbody.
if (hit.rigidbody) {
hit.rigidbody.AddExplosionForce(explosionPower, transform.position, explosionRadius); //Add an explosion force that affects all rigidbody's.
}
}
PoolManager.Pools["Effects"].Spawn(explosion.transform, transform.position, transform.rotation);
PoolManager.Pools["EBullets"].Despawn(this.transform);
despawned = true;
}
}