Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 SteelArrow21 · Apr 18, 2014 at 09:09 PM · new rect

GUI.DrawTexture C#

So I have this line of code:

     void OnGUI(){
       GUI.color = new Color (1, 1, 1, alpha);
       GUI.depth = drawDepth;
       GUI.DrawTexture (new Rect (10, 10, 10, 10), fadeTexture, ScaleMode.StretchToFill, true);
     }

But, when ever the function happens, I get this error:

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) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:208) UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, ScaleMode scaleMode, Boolean alphaBlend) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:189) FadeOutWhiteC.OnGUI () (at Assets/Custom Assets/Scripts/FadeOutWhiteC.cs:28) FadeOutWhiteC.Update () (at Assets/Custom Assets/Scripts/FadeOutWhiteC.cs:16)

What must I do to avoid this error?

Comment
Add comment · 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 Socke97 · Apr 18, 2014 at 09:37 PM 0
Share

Do you have something like

 public Texture fadeTexture;

?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Burla · Apr 18, 2014 at 09:40 PM

In the code you provided you didn't declare fadeTexture ? I guess you need this code at the top of your script.

 Public Texture fadeTexture;

And then you should drag and drop the texture into the field in the editor.

Let me know if this helped.

Comment
Add comment · Show 1 · 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 SteelArrow21 · Apr 18, 2014 at 09:46 PM 0
Share

Well that wasn't the whole code. Earlier in the script I declared the texture. Here's the whole code:

 using UnityEngine;
 using System.Collections;
 
 public class FadeOutWhiteC : $$anonymous$$onoBehaviour {
 
     public Texture2D fadeTexture;
     public float fadeSpeed = 0.2f;
     public int drawDepth = -1000;
     public GameObject door;
 
     private float alpha = 0.0f;
     private int fadeDir = -1;
 
     void Update(){
         if (door == null)
                         OnGUI ();
     }
 
     void OnGUI(){
 
                         alpha -= fadeDir * fadeSpeed * Time.deltaTime;  
                         alpha = $$anonymous$$athf.Clamp01 (alpha);   
         
                         GUI.color = new Color (1, 1, 1, alpha);
         
                         GUI.depth = drawDepth;
         
                         GUI.DrawTexture(new Rect(10, 10, 10, 10), fadeTexture, Scale$$anonymous$$ode.StretchToFill, true);
 
     }
 }
avatar image
1

Answer by Bunny83 · Apr 19, 2014 at 02:40 PM

From your call stack of your error (and additionally from your comment) we can see that you call OnGUI manually from Update. This will break the whole GUI stuff. You never call OnGUI manually, it's called automatically by unity, so remove the call from Update.

Also like Burla said, make sure you assigned a texture to the variable in the inspector.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using predefined colors vs defining new colors 1 Answer

WHERE´S THE ERROR?PLEASEEE 1 Answer

Using C++ libraries for Editor extensions 0 Answers

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

C# - Setting color of a rect on creation 2 Answers


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