• 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
Question by Cobradabest · Feb 03, 2013 at 12:00 PM · c#errorguihud

"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);
         }
         else
         {
         }
         GUI.DrawTextureWithTexCoords(new Rect(0, 0, 1, 1)
             , texture, new Rect(0,0, 1, health), false);
     }

How do I fix this?

Comment

People who like this

0 Show 1
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 gRntaus · Feb 03, 2013 at 12:05 PM 0
Share

Just out of interest does changing the texture Texture for Texture to Texture2D to anything?

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by paulaceccon · Feb 03, 2013 at 12:07 PM

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

Comment

People who like this

0 Show 6 · 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 Cobradabest · Feb 03, 2013 at 12:14 PM 0
Share

Oh yeah, that's right, there's an "OnGUI" function.

It's not any errors anymore but nothing's showing up now, what could cause that?

avatar image paulaceccon · Feb 03, 2013 at 12:22 PM 0
Share

Reading you question, I didn't find something that could cause this. Can you test this code below?

    public enum BarType
    {
        Health = 0,
        Armour = 1,
        Power = 2
     }
     public BarType barType;
     public Texture texture;
     public GameManager masterScript;
     private float health;
  
    void Awake ()
    {
         texture =  Resources.Load("yourtexturename") as Texture;
    }
 
     void Update () 
     {
        if (barType == BarType.Health)
        {
          health = (masterScript.health / masterScript.maxHealth);
        }
        else
        {
        }
     }
 
 void OnGUI ()
 {
    if (texture)
         GUI.DrawTextureWithTexCoords(new Rect(0, 0, 1, 1)
          , texture, new Rect(0,0, 1, health), false);
 
    else
      Debug.Log("texture not found");
 }

You just have to create a Resources folder and put your texture there. Another thing, if this condition

  if (barType == BarType.Health)
  {
       health = (masterScript.health / masterScript.maxHealth);
  }

didn't apply, what is the health value?

avatar image Cobradabest · Feb 03, 2013 at 12:28 PM 0
Share

I'm not too into the idea of using "Resources.Load", because that would mean the script would be exclusive to that texture, and I don't want that, I have other bars in the game, and using the same script for all of them would really save me trouble.

avatar image paulaceccon · Feb 03, 2013 at 12:35 PM 0
Share

I see, I'm just trying to figure out if Unity is finding your texture. Have you put some Debug.Log s to see if the texture was found?

avatar image Cobradabest · Feb 03, 2013 at 12:43 PM 0
Share

Alright, I've tried the code anyway, and I'm afraid it didn't make a difference.

After taking the "Resources.Load" line out, I decided to test it again, I would expect it to log that the texture is missing, and that's what it did, so it's definetly accessing the function, it's not drawing anything when there IS a texture.

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Problems with GUI and Resolutions. (C#) 1 Answer

Is there anyway to only show a part of a GUI image during the game? (C#) 1 Answer

Make it Spawn 1 Cube instead of infenity 1 Answer

OnInspectorGUI() Getting Control 2's position in a group with only 2 controls when doing repaint 2 Answers

GUISttyle NullReferenceException error. what am I missing ? 1 Answer


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