Accessing Enemy lifebar prefab

I created a guitexture, made it prefab and attached it, as a child, to an enemy.
I want to be able to access it, and use it as a lifebar that appears when you make that enemy your target, but I’ve having trouble doing so. Do I need to instantiate the prefab first or do I need to find the child of the parent object?

At some point, I need to access the pixelInset to change the length of the bar. There must be some things I haven’t learned yet. Other than the Burgzerg arcade, any other in-depth tutorials?

Thanks!

You must not child GUITexture (nor GUIText) to a 3D world object: as a child, it will follow the parent position - but transform.position has a different meaning for these items: it’s expressed in viewport coordinates, which range from 0 to 1, thus any small parent movement will kick the GUITexture out of screen. You should instead link the GUITexture to the enemy or vice-versa via script using a reference variable, and calculate its position in screen with Camera.WorldToViewportPoint.

If you want to have a health bar for each enemy, instantiate the enemy and place the health bar instantiation code in the enemy script: it may instantiate the health bar prefab and keep it disabled when not needed - something like this:

Enemy script:

var health: float; // enemy health
var maxHealth: float = 100; // full health value
var hBarWidth: float = 150; // health bar width in pixels
var offset: float = 2; // bar vertical offset above enemy (world units)
var hBarPrefab: GUITexture; // drag the bar prefab here in the enemy prefab
private var hGuiTex: GUITexture; // link to the health bar

function Start(){ 
  hGuiTex = Instantiate(hBarPrefab); // create the health bar
  hGuiTex.enabled = false; // make it invisible
  health = maxHealth; // enemy comes to the world full of health
}

function Update(){
  if (hGuiTex.enabled){ // if health bar visible...
    // calculate its size:
    hGuiTex.pixelInset.width = hBarWidth * health / maxHealth;
    // find the 3D bar position offset units above enemy:
    var pos3D = transform.position + Vector3.up * offset;
    // convert it to viewport coordinates and set bar position:
    hGuiTex.transform.position = Camera.main.WorldToViewportPoint(pos3D);
  }
}

function EnableBar(onOff: boolean){
  hGuiTex.enabled = onOff;
}

function OnDestroy(){ 
  // send health bar to hell with the enemy:
  Destroy(hGuiTex.gameObject);
}

When you select some enemy as a target, use any reference to it with SendMessage to enable/disable its health. Supposing for instance that the variable curTarget holds a reference to the currently selected enemy (player script):

private var curTarget: Transform;
private var oldTarget: Transform;

function Update(){
  if (curTarget != oldTarget){ // if target changed...
    // if there exists an oldTarget, disable its bar:
    if (oldTarget) oldTarget.SendMessage("EnableBar", false);
    // enable curTarget health bar:
    curTarget.SendMessage("EnableBar", true);
    oldTarget = curTarget; // update oldTarget
  }
}