Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VIannone · Jul 15, 2013 at 12:23 AM · guitexturegraphicshuddraw

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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 :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VIannone · Jul 15, 2013 at 01:48 AM 0
Share

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.

avatar image Psyco92 · Mar 06, 2014 at 06:41 PM 0
Share

what about in my case where i need to use Graphics.DrawTex ins$$anonymous$$d, due to a shader i need on the texture?

avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fill rectangle from bottom to top 1 Answer

Replacing Gui.DrawTexture or gui textures in C#? 0 Answers

How to call Graphics.DrawTexture() in Update 1 Answer

How to use Graphics.Draw draw a texture behind other GameObject? 2 Answers

Can you use GUI.DrawTexture to make a clickable button 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges