- Home /
Show only selected enemy's health bar
'Ello all. I've been 'aving a bit of an issue with the above. I kind of wonder if I'm just trying to access the variable the wrong way.
'ere's where I'm trying to access it in the targetting script:
private void SelectTarget()
{
//We'll change the selected target's color to red, so we can tell it apart.
selectedTarget.renderer.material.color = Color.red;
EnemyHealth2 st = (EnemyHealth2)GetComponent("EnemyHealth2");
st.GetComponent<EnemyHealth2>().isSelected = true;
PlayerAttack2 pa = (PlayerAttack2)GetComponent("PlayerAttack2");
pa.target = selectedTarget.gameObject;
}
private void DeselectTarget()
{
//We'll change the color back to white.
selectedTarget.renderer.material.color = Color.white;
selectedTarget = null;
EnemyHealth2 st = (EnemyHealth2)GetComponent("EnemyHealth2");
st.GetComponent<EnemyHealth2>().isSelected = false;
}
'ere's the bit containing the variable in my enemy 'ealth script. The bool is declared up top and initially set to false.
void OnGUI()
{
if(isSelected = true)
{
//Set up a box to hold our health bar.
GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
}
}
Answer by aldonaletto · Sep 05, 2012 at 11:06 PM
You're doing a big mess when accessing the selected target EnemyHealth2 script. You must have a reference to the selected target in order to get one of its components - and selectedTarget is this reference:
private void SelectTarget()
{
//We'll change the selected target's color to red, so we can tell it apart.
selectedTarget.renderer.material.color = Color.red;
// use selectedTarget as the GetComponent reference:
selectedTarget.GetComponent< EnemyHealth2>().isSelected = true;
// without a reference, the owner object is assumed - in this
// case, PlayerAttack2 must be attached to the same object as
// this script:
PlayerAttack2 pa = (PlayerAttack2)GetComponent("PlayerAttack2");
pa.target = selectedTarget.gameObject;
}
private void DeselectTarget()
{
//We'll change the color back to white.
selectedTarget.renderer.material.color = Color.white;
selectedTarget.GetComponent< EnemyHealth2>().isSelected = false;
selectedTarget = null; // assign null after using selectedTarget!
}
Thanks. I've inputted the code, but it's doing the same as before I made the changes you gave (showing all enemy 'ealth bars, not just selected. When attacking one, you can see a bar go down behind, 'owever you can see at least one still full in front of the depleted.).
Am I messing something up in the 'ealth script (I can post the rest if needed), or is it perhaps because I have the same 'ealth script attached to several enemy objects, and it's thus setting all to true (or something similar)?
The script attached to an object is an independent instance: each one has its own variables, which aren't altered when other instances of the same script are modified (except static variables). But I suspect that your problem is in the OnGUI code, in this line:
if (isSelected = true)
This if is always true, because "=" is the assignment operator: any assignment in JS and CS returns the value assigned, thus this if will always see a true! You should use "==" ins$$anonymous$$d, or just:
if (isSelected)
You definitely don't need to compare a boolean variable to true: this comparison always returns the variable value!
Alright, thanks. Can't believe I messed that one up... Just goes to show 'ow rusty I've really become, I guess :P