- Home /
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!
Answer by Cerbion_ · Jul 15, 2013 at 01:28 AM
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 :)
Hey Cerbi,
Sorry, it didn't work. I'm getting a new error now, though:
UnassignedReferenceException: The variable tex of 'RenderHealthAndOtherGUI' has not been assigned. You probably need to assign the tex variable of the RenderHealthAndOtherGUI script in the inspector. UnityEngine.Texture.get_width () UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, Scale$$anonymous$$ode scale$$anonymous$$ode, Boolean alphaBlend, Single imageAspect) UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image) RenderHealthAndOtherGUI.OnGUI () (at Assets/RenderHealthAndOtherGUI.js:14)
I most certainly assigned the reference in the inspector. It's there when I look at the instantiated clone in the scene view. I still don't know what the problem is.
Thanks for your help thus far, Cerbi.
what about in my case where i need to use Graphics.DrawTex ins$$anonymous$$d, due to a shader i need on the texture?
Answer by VIannone · Jul 17, 2013 at 01:56 AM
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...