- Home /
How to hide the enemy's health bar until the player enters the enemy collier?
I need help on this problem; this problem involves with GUITexture as enemy health. When the game starts I always see the enemy's health bar and I'm not even close to one. Any ideas would be appreciated.
Answer by OP_toss · Apr 09, 2013 at 11:17 PM
Place a Collider on your player.
Mark it as Trigger.
Add a script to the player.
In the script add the following methods:
protected void OnTriggerEnter( Collider other ) { //if other is enemy, display enemy health bar }
protected void OnTriggerExit( Collider other ) { //if other is enemy, hide enemy health bar }
Answer by GamerBot · Apr 10, 2013 at 01:19 AM
Thank you for that. I honestly can't believe that was the issue. Thank you.
I try but nothing work so far. Is there a function that hides the GUI or something like that because I'm been on this for 12 times now. Here is my code for the advice you give me and my code for the enemy's health.
var sight : boolean;
function OnTriggerEnter(other : Collider){
if(other.tag == "$$anonymous$$onster"){
sight = true;
}
}
function OnTriggerExit(other : Collider){
if(other.tag == "$$anonymous$$onster"){
sight = false;
}
}
function Start () {
}
function Update () {
if(sight){
}
}
var health : int;
var takeDamage : boolean;
var rangeField : boolean;
var healthFull : boolean;
var GUIHealhBar : Texture2D;
var GUIHp3 : Texture2D;
var GUIHp2 : Texture2D;
var GUIHp1 : Texture2D;
var barWidth : int;
var barHeight: int;
//Range of the enemy field.
//When the player enters the field.
function OnTriggerEnter(other : Collider){
if(other.tag == "Player"){
takeDamage = true;
}
}
//When the player leaves the field.
function OnTriggerExit(other : Collider){
if(other.tag == "Player"){
takeDamage = false;
}
}
function Start () {
}
function Update () {
//When the enemy gets hit by the player's blaster it losses health.
if(takeDamage){
if(Input.GetButtonDown("Fire1")){
health --;
}
}
}
//When the enemy losses all the health it gets destroy.
if(health < 1){
print("Enemy Down");
health = 0;
Destroy(gameObject);
}
//The enemy's health is full.
if(health == 4){
healthFull = true;
}
//The state of the enemy's health.
function OnGUI(){
if(health == 4){
GUI.Label(Rect(10,40,barWidth, barHeight),GUIHealhBar);
}
if(health == 3){
GUI.Label(Rect(10,40,barWidth, barHeight),GUIHp3);
}
if(health == 2){
GUI.Label(Rect(10,40,barWidth, barHeight),GUIHp2);
}
if(health == 1){
GUI.Label(Rect(10,40,barWidth, barHeight),GUIHp1);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613100007im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[SOLVED] Object detection inside a GUI area 1 Answer
Realistic Health Bar 3 Answers
How can i show the texture in the array? 2 Answers
OnGui Function help 1 Answer
Repeat fade in and out 1 Answer