Please help me fix this collsion/health/gui script JS

Ok I am trying to make my player’s health go down when he collides with the blob prefab in my scene. I have 20 2D GUI textures that slowly should decrease to 0 when the monster collides with the player.

Scripts:

player_Health

#pragma strict
// Health Textures

var health20 : Texture2D;
var health19 : Texture2D;
var health18 : Texture2D;
var health17 : Texture2D;
var health16 : Texture2D;
var health15 : Texture2D;
var health14 : Texture2D;
var health13 : Texture2D;
var health12 : Texture2D;
var health11 : Texture2D;
var health10 : Texture2D;
var health9 : Texture2D;
var health8 : Texture2D;
var health7 : Texture2D;
var health6 : Texture2D;
var health5 : Texture2D;
var health4 : Texture2D;
var health3 : Texture2D;
var health2 : Texture2D;
var health1 : Texture2D;

// Intergers

static var player_Health = 20;

// Textures

var healthGui : GUITexture;

function Start () {

}

function Update () {

switch (player_Health)
{
case 20:
healthGui.texture = health20;
break;

case 19:
healthGui.texture = health19;
break;

case 18:
healthGui.texture = health18;
break;

case 17:
healthGui.texture = health17;
break;

case 16:
healthGui.texture = health16;
break;

case 15:
healthGui.texture = health15;
break;

case 14:
healthGui.texture = health14;
break;

case 13:
healthGui.texture = health13;
break;

case 12:
healthGui.texture = health12;
break;

case 11:
healthGui.texture = health11;
break;

case 10:
healthGui.texture = health10;
break;

case 9:
healthGui.texture = health9;
break;

case 8:
healthGui.texture = health8;
break;

case 7:
healthGui.texture = health7;
break;

case 6:
healthGui.texture = health6;
break;

case 5:
healthGui.texture = health5;
break;

case 4:
healthGui.texture = health4;
break;

case 3:
healthGui.texture = health3;
break;

case 2:
healthGui.texture = health2;
break;

case 1:
healthGui.texture = health1;
break;

}

}

/////////// player_Health_Controls (different script)

#pragma strict

function Start () {

}

function Update () {

}

function OnTriggerEnter (col : Collider)
{
if(col.gameObject.name == “Blob”)
{

player_Health.player_Health -=1;
}

}


If you’re going to use 20 different textures, I would get rid of the individual Texture2D variables, and get rid of your switch statement. Use a Texture2D array instead and do something like this:

var healthTextures : Texture2D();

Assuming your player GameObject is named ‘Player’…

(I also would change your player_Health script to a different name, or change the integer variable name. Its not a great idea to have a class name the same as a variable name.)

/////////// player_Health_Controls (different script)

#pragma strict

static var playerHealth : player_Health;

function Start () {
playerHealth = GameObject.Find("Player").GetComponent("player_Health");
}

function Update () {

}

function OnTriggerEnter (col : Collider)
{
if(col.gameObject.name == "Blob")
{
playerHealth.player_Health -=1;
healthGui.texture = healthTextures[playerHealth.player_Health];
}

}

Forgive me if there are syntax errors, I don’t use UnityScript.

I tried but failed. ;/ I guess I will find a better tutorial for health. Thanks

My method is a very different one (that i found in a youtube video! can’t remember name to credit)

I don’t use textures, or GUI, but a cube, whose scale i adjust using a Vector3.Lerp to adjust according to the health value/variable. Works perfectly.

Good plan.

Sounds Interesting let me know if you find the video.

Just google youtube health bar unity or something like that… i would if i have time