"NullReferenceException" while trying to draw a texture (C#)

I’m having a bit of a problem with trying to draw a GUI Texture, I’m trying to draw a lifebar, and the end gets cut off as your health goes down until eventually it, and your health, is nothing.

That’s the theory, but like I said, I’ve come across a problem, I’m getting the “NullReferenceException” error, even though nothing I’m referencing is null, at least not that I know of.

After trial and error, I’ve concluded that in the draw call, it’s the texture part that’s causing the problem. I have it set to a public value, so that in the inspector, I can drag and drop the image into the slot. It also means I could reuse the script for other GUI/HUD Bars, which would really save me time in the long run, so I really hope this isn’t the problem.

Here’s my code:

	public enum BarType
		Health = 0,
		Armour = 1,
		Power = 2
	public BarType barType;
	public Texture texture;
	public GameManager masterScript;
	private float health;
	void Update () 
		if (barType == BarType.Health)
			health = (masterScript.health / masterScript.maxHealth);
		GUI.DrawTextureWithTexCoords(new Rect(0, 0, 1, 1)
			, texture, new Rect(0,0, 1, health), false);

How do I fix this?

Have you tried to put this

GUI.DrawTextureWithTexCoords(new Rect(0, 0, 1, 1), texture, new Rect(0,0, 1, health), false);

inside the OnGUI function, instead of in the Update??