GUI.Label not updating image

The health bar has 20 states so I use math.round to find the nearest image to load. I assign the textures in the inspector and I know the script can access it because it works for the initial load but doesn’t update thereafter. The code seems to be spot perfect and I can see the healthBar variable that stores the texture changing but the GUI.Label that is getting its image from this variables don’t change properly. For example if I set my code to different values beforehand in the code or in the inspector the health bar updates the image properly when I play but it will never change after that even though I can see the healthBar variable updating that the method derives from changing to the right image.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerControl : MonoBehaviour {
	public int health;
	public int i;
	public List<Texture2D> textures = new List<Texture2D>();
	public Texture2D healthBar;
	// Use this for initialization
	void Awake () 
	{
		health = 10;
		i = 2;

	}
	
	// Update is called once per frame
	void Update () 
	{
		if ( health < 0)
			Application.LoadLevel(Application.loadedLevel);
		i =	Mathf.RoundToInt(health / 5);

	}
	void OnGUI ()
	{
		healthBar = textures*;*
  •  GUI.Label(new Rect(0, -50, 128, 128) , healthBar);*
    
  • }*
    }

Well I got it to work like this but this seems a bit ineffiecent to me and still doesn’t explain why my previous code wasn’t working. Is this another one of those bugs?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerControl : MonoBehaviour {
	public int health;
	public int i;
	public List<Texture2D> mores = new List<Texture2D>();
	public Texture2D[] things = new Texture2D[21];
	public Texture2D healthBar;
	// Use this for initialization
	void Start () 
	{
		foreach ( Texture2D thing in things)
			mores.Add(thing);
		health = 100;
		i = 20;

	}
	
	// Update is called once per frame
	void Update () 
	{
		if ( health < 0)
			Application.LoadLevel(Application.loadedLevel);
		i =	Mathf.RoundToInt(health / 5);


	}
	void OnGUI ()
	{

		GUI.Label(new Rect(0, -50, 128, 128) , mores*);*
  • }*
    }

@237641: Your 1st code works fine. I tried to run it and healthBar texture was changing as per expectations. I dragged the value of int health from 0 to 99 in the inspector in play mode, the corresponding value of i was changing correctly thus was displaying the correct textures which you’ve assigned to healthBar in OnGUI() function.
I’ll also suggest you to change the y-coordinate of GUI.Label to a value greater than 0. Your GUI.Label is 128 by 128 but the y-coordinate of top-left edge is -50 thus some part of your texture might be clipping or whole of it if the texture itself is smaller than 50px.