Help with GUI !!

Hey Guys,
I was wondering if you could help me with something. Well basically I want to create a GUI using scripting not unity inbuilt GUI. What i want to do is add a image to this GUI using an array. The array will hold about more than 20 images and what i have in mind is that if the player presses the Space Key the GUI gets the first image in the array and then deletes that image. Now is this possible? Because i know you can make a variable for the GUI on the top called GUI style something like that. But that just places the image on the GUI and allows you to make hover effects and ETC. I dont want that because i want my health bar to animate. And in order to do that i was thinking if i create a array which holds 2d graphics i can then call them for my GUI and its start go trough the array until it reaches the last 2d graphics. which will be zero health. Please can someone help me with this. i have pasted my script below so far on what i have done thank you so much in advance :slight_smile:

var positionx : float;
var SKIN = GUIStyle;
var LifeAuto : boolean = true;

function Update(){

		positionx -= 5 * Time.deltaTime;
		LifeAuto = false;
	if( positionx == 70){
		LifeAuto = false;
	if( positionx == - 88){
		LifeAuto = true;

function OnGUI(){

	GUI.Box(new Rect(10 , 20 , 200+positionx,100),"");

function Timer(){

	if(LifeAuto == true){
	   yield WaitForSeconds(5);
		positionx ++  ;
		if( positionx > 70){
		   positionx = 70;

You can use GUI.DrawTexture and select the texture to draw from an array, exactly like you’re willing to do:

var healthArray: Texture[];
var curHealth: int = 0;
var altLife: boolean = false;  // animates health bar when true
var spriteDel: float = 0.2;  // duration of each sprite

function OnGUI(){

  var nImage: int;
  if (altLife){
	nImage = Mathf.Repeat(Time.time/spriteDel, healthArray.Length);
  } else {
    nImage = Mathf.FloorToInt((curHealth-1) * (healthArray.Length-1)/100.0)+1;
  GUI.DrawTexture(Rect(10,20,200,30), healthArray[nImage]);

EDITED: Script modified to have 0 health too, and to fix bug when health == 100.

This calculate the image index based on the curHealth (0 - 100). You should create all images with the same size, then set the array size in the Inspector and drag the images to the empty slots. Image 0 now corresponds to 0 health, and the other images will be distributed equally from 1% to 100%.

EDITED #2: Modified to allow health bar animation when altLife is true.