x


Drawing a texture results in nullreferenceexception

Hi, I'm creating a 1x1 semitransparent white texture and using it to fill a 100x100 rectangle. This is the code

public void Update()
    {
        Texture2D seltext = new Texture2D(1, 1, TextureFormat.ARGB32, false);
        Color col = new Color(1, 1, 1, (float)0.2);
        seltext.SetPixel(1, 1, col);
        seltext.Apply();
        GUI.DrawTexture(new Rect(100, 100, 100, 100), seltext);
    }

but when i run the game i get NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, ScaleMode scaleMode, Boolean alphaBlend, Single imageAspect) UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image)

why?

more ▼

asked Aug 13 '10 at 09:51 AM

Patrik gravatar image

Patrik
59 13 13 20

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can't put GUI functions in Update. Also, creating a texture every frame is not a good idea.

--Eric

more ▼

answered Aug 13 '10 at 10:37 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

Oh, how dumb of me. thank you, that did it

ps. yes, yes i know i should not create a texture every frame. it was just to see if it worked

Aug 13 '10 at 10:46 AM Patrik

Hey Eric does that include putting it in a method that is invoked inside of update? Because I have that updates a score texture which counts up so it appears inside an update display method.

Jan 24 '11 at 01:31 PM .hooligan

I get the same error but I've moved my script outside of any Update methods?

Jan 24 '11 at 01:34 PM .hooligan

You can only use GUI functions in OnGUI.

Jan 24 '11 at 03:45 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2210
x399
x20

asked: Aug 13 '10 at 09:51 AM

Seen: 1830 times

Last Updated: Aug 13 '10 at 09:51 AM