Getting Error "DrawGUITexture: Texture is Null" when using Graphics.DrawTexture in OnGUI()

Hey Guys,

Thanks for the help in advance. I’m trying to make a health bar using a cutout material, but that’s beside the point. For whatever reason, I can’t seem to be able to simply draw a texture using “Graphics.DrawTexture” and assigning a texture through the inspector. Here’s the code (javascript):

#pragma strict
var x : int;
var y : int;
var width : int;
var height : int;
var box : Rect = new Rect(x, y, width, height);
var tex : Texture2D;
var mat : Material;

function OnGUI(){

	if(Event.current.type.Equals(EventType.Repaint)){
 		Graphics.DrawTexture(box, tex);
 
 	}
}

I’ve tried using a material, omitting the material, changing the texture I use to something that I know renders, etc… I’ve been troubleshooting for hours and can’t seem to find the problem. Here’s the error I’m getting in the console:

DrawGUITexture: texture is null
UnityEngine.Graphics:DrawTexture(Rect, Texture)
RenderHealthAndOtherGUI:OnGUI() (at Assets/RenderHealthAndOtherGUI.js:15)

Yes, I’ve made sure to assign the texture in the inspector. The object this is attached to in an instantiated clone, but I don’t think that should have an effect, right?

The documentation pages aren’t clearing anything up for me either. Any help is very much appreciated. I don’t know why I’m stumped on such a simple issue. Thanks!

Let me quote the official documentation here:

If you want to draw a texture from inside of OnGUI code, you should only do that from EventType.Repaint events. It’s probably better to use GUI.DrawTexture for GUI code.

I’d suggest you test that out just write below the OnGUI function: (without the repaint event)

GUI.DrawTexture(box, tex);

If that doesn’t do the trick, please reply :slight_smile:

OH HECK!

I figured it out!

Jeez, you think you’re a good programmer and then you do something stupid like this. I had the script attached to two different objects without knowing it. One had everything assigned in the inspector and the other was blank. That’s why I could get a Null Reference and see the object assigned in the inspector at the same time.

Thanks. Sorry about that question…