• 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 Patrik · Aug 13, 2010 at 09:51 AM · texturenullreferenceexceptionrectangle

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?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Eric5h5 · Aug 13, 2010 at 10:37 AM

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

--Eric

Comment
Add comment · Show 4 · 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 Patrik · Aug 13, 2010 at 10:46 AM 0
Share

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

avatar image .hooligan · Jan 24, 2011 at 01:31 PM 0
Share

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.

avatar image .hooligan · Jan 24, 2011 at 01:34 PM 0
Share

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

avatar image Eric5h5 · Jan 24, 2011 at 03:45 PM 0
Share

You can only use GUI functions in OnGUI.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Why is there no texture on my terrain after I build the game? 1 Answer

Resources.LoadAll returning null but everything seems right 1 Answer

Crash on Texture Select 2 Answers

Assigning UV Map to model at runtime 0 Answers

Resources.Load(path+name) as texture == null reference 7 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges