Lifebar Gui issue

I made a script for a lifebar, and it kinda works, except for the fact that it won't change when lifes are lost..

Here's my script:

var itemTracker;
itemTracker = GameObject.FindWithTag("GameController").GetComponent(GameStats);

var positionFromTop = 10;
var positionFromLeft = 10;

var textureWidth = 100;
var textureHeight = 50;

var lifeBarTexture;

var hundredPercentLife : Texture ;
var ninetyPercentLife : Texture ;
var eightyPercentLife : Texture ;
var seventyPercentLife : Texture ;
var sixtyPercentLife : Texture ;
var fiftyPercentLife : Texture ;
var fourtyPercentLife : Texture ;
var thirtyPercentLife : Texture ;
var twentyPercentLife : Texture ;
var tenPercentLife : Texture ;

function Update()
{
    if(itemTracker.playerLife > 90)
        lifeBarTexture = hundredPercentLife;
    if(itemTracker.playerLife > 80 && itemTracker.playerLife < 91)
        lifeBarTexture = hundredPercentLife;
    if(itemTracker.playerLife > 70 && itemTracker.playerLife < 81)
        lifeBarTexture = hundredPercentLife;
    if(itemTracker.playerLife > 60 && itemTracker.playerLife < 71)
        lifeBarTexture = hundredPercentLife;
    if(itemTracker.playerLife > 50 && itemTracker.playerLife < 61)
        lifeBarTexture = hundredPercentLife;
    if(itemTracker.playerLife > 40 && itemTracker.playerLife < 51)  
        lifeBarTexture = hundredPercentLife;
    if(itemTracker.playerLife > 30 && itemTracker.playerLife < 41)
        lifeBarTexture = hundredPercentLife;
    if(itemTracker.playerLife > 20 && itemTracker.playerLife < 31)
        lifeBarTexture = hundredPercentLife;
    if(itemTracker.playerLife > 10 && itemTracker.playerLife < 21)
        lifeBarTexture = hundredPercentLife;
    if(itemTracker.playerLife > 0 && itemTracker.playerLife < 11)
        lifeBarTexture = hundredPercentLife;
}

function OnGUI() 
    {
        GUI.Box(Rect(positionFromLeft, positionFromTop, textureWidth, textureHeight), lifeBarTexture);
    }

Its fairly obvious that you need to do something different in every if sentence, instead of doing the same

lifeBarTexture = hundredPercentLife

In everyone of them.

Unrelated side notes:

is this code run in your start method?

itemTracker = GameObject.FindWithTag("GameController").GetComponent(GameStats);

Also, why arent you using if-else if instead of having all those ifs?

if(itemTracker.playerLife > 90) {     
    lifeBarTexture = hundredPercentLife;    
} else if(itemTracker.playerLife > 80 && itemTracker.playerLife <= 90) {
    ninetyPercentLife = ***ninetyPercentLife***;
} else if( ...
    etc..

Agreed.

As a side comment, consider a dynamic approach instead of 10 different static textures. I imagine you want something like the lifebar turning increasingly red the closer it is to 0, and perhaps green at 100?

Instead of inserting a different texture every 10th percent, you could set the width of a box, and color a single texture gradually. Draw a box with GUI.Box and scale the width of it and its texture according to health every frame.

Here's a code snippet that draws a completely green box for you:

    Texture2D boxTex = new Texture2D(300, 30);

    GUIStyle style = new GUIStyle();
    style.padding = new RectOffset(0, 0, 0, 0);
    style.normal.background = boxTex;

    for (int i = 0; i < boxTex.width; i++)
    {
        for (int j = 0; j < boxTex.height; j++)
        {
            boxTex.SetPixel(i, j, new Color(0, 1, 0, 1));
        }
    }
    boxTex.Apply();

    GUI.Box(new Rect(100, 400, 300, 30), "", style);

Then you just multiply the width with something like Player.health, if it's a float between 0 and 1, or whatever you have. Then, similarly, you can cause the red and green components of the associated color to switch between 1 and 0 respectively, so that the texture becomes redder and redder closer to 0.

This approach is more elegant code-wise, saves memory for textures, but is a little more expensive for the CPU due to the pixel-traversal.