GUI.DrawTexture() - Object Reference not an instance

I’m having problems creating a simple 2D texture on screen. This is what i’m doing:

Creating a script that has a member property that’s a Texture2D type.

Adding an Empty GameObject to the scene, adding my script to the GameObject and setting the texture property of the script to reference the image in the assets folder.

So, all is well, i’ve put the script in the scene and the script knows which image to use as its texture.

But when i call GUI.DrawTexture(rect, texture) i get

NullReferenceException: Object reference not set to an instance of an object

Logging the value of the Texture tells me it’s an instance of Texture2D and the Rect is ok, too. So i’m not sure what it’s telling me is not instantiated?

Can anyone shed some light on this for me?

T

A common problem, but in this case it sounds a bit strange. Can you post the code to help debug?

The project it’s part of is huge (i’m just making some alterations) - so i’ve created and attached a simple project to demonstrate the error.

If you could point out where i’m going wrong that’d be great.

237068–8482–$texture2dexample_160.zip (1.07 MB)

I almost missed it…

You need to call GUI functions from the OnGUI method, not from the Update method.

oops, thanks!