So I am currently trying to update my health bar GUI every time my main character collides with an object tagged “Hazard”. I have a main character controlled with a Character Controller and a Stove with a Box Collider and Rigidbody so that I may text it. I have this code as my GUI for the health bar:
//Variables for the texture to render on screen
var fullHealth : Texture2D;
var fourHealth : Texture2D;
var threeHealth : Texture2D;
var twoHealth : Texture2D;
var oneHealth : Texture2D;
var currentHealth : Texture2D;
//Item texture variables
var itemOne : Texture2D;
var itemTwo : Texture2D;
var itemThree : Texture2D;
var itemFour : Texture2D;
var itemEmpty : Texture2D;
//boolean variables for if items are found or not
var oneIsFound : boolean = false;
var twoIsFound : boolean = false;
var threeIsFound : boolean = false;
var fourIsFound : boolean = false;
//position variables
var position : Rect;
var positionItemOne : Rect;
var positionItemTwo : Rect;
var positionItemThree : Rect;
var positionItemFour : Rect;
function Start() {
//set the health to the top of the screen
position = Rect(0, 0, 100, 50 );
positionItemOne = Rect (480, 10, 60, 50);
positionItemTwo = Rect (480, 60, 60, 50);
positionItemThree = Rect (480, 110, 60, 50);
positionItemFour = Rect (480, 160, 50, 60);
//set the default health to Full
currentHealth = fullHealth;
}
function OnGUI() {
//Draw the current health bar
GUI.DrawTexture( position, currentHealth );
//draw the Items that are found when they are found
if(oneIsFound){
GUI.DrawTexture( positionItemOne, itemOne );
}
else{
GUI.DrawTexture( positionItemOne, itemEmpty );
}
if(twoIsFound){
GUI.DrawTexture( positionItemTwo, itemTwo );
}
else{
GUI.DrawTexture( positionItemTwo, itemEmpty );
}
if(threeIsFound){
GUI.DrawTexture( positionItemThree, itemThree);
}
else{
GUI.DrawTexture( positionItemThree, itemEmpty );
}
if(fourIsFound){
GUI.DrawTexture( positionItemFour, itemFour );
}
else{
GUI.DrawTexture( positionItemFour, itemEmpty );
}
}
And then I have this code in my main Character to determine the Collision:
function Start () {
}
function Update () {
}
function OnTriggerEnter (collision: Collider){
if(collision.gameObject.tag == "Hazard"){
Debug.Log("COllision");
}
}