- Home /
"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?
Just out of interest does changing the texture Texture for Texture to Texture2D to anything?
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??
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?
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 Game$$anonymous$$anager 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?
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.
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?
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.