I Instantiate GO, pass over variables but need a check (if) that does not exist yet

Hi,

Another I cant figure out…

I have a homing missile and target is passed over from car trigger.

I use ontrigger exit for death + myTransform.Translate(Vector3.up * 10000) so it gets called.

Now guess what, 1 or 2 homing missiles follow it up after its destroyed! Looks funny though…

I can’t put an if statement as the target wont exist yet, so below wont work:

if(myTarget.health > 0){
			transform.LookAt(myTarget);
		}

UnityEngine.Transform’ does not contain a definition for `health’

Thanks for any help.

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.

Sorry should of put some more code in.

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;
		}
	}

Let me know if you need anymore info.

Thanks.

If you want to check the health, you need to access your target’s component that has the health.

if(myTarget.GetComponent<MyComponentWithTheHealth>().health > 0){
    transform.LookAt(myTarget);
}

But you don’t want to call GetComponent all the time. Instead, could have your missile keep a reference to both the transform and the health script.

if(myTargetComponentWithHealth.health > 0){
    transform.LookAt(myTargetTransform);
}

Alternatively, if you deactivate the dead object, you could just check whether’s its still active.