• 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
Question by Ria Ayo · Jun 02, 2013 at 12:33 AM · texture2donguigui.drawtexture

"null texture passed" for any texture2D other than the 2 originals.

GUI.DrawTexture working for 2 textures, but not for others. Code functions fine otherwise, but it is giving "null texture passed" for any texture2D other than the 2 originals.

I'm afraid I simply have not been able to find a copy of this same problem, so I apologize if it exists somewhere already.

I am having an issue when using the GUI.DrawTexture in the function OnGUI to create "cast bars" that essentially have a static bar texture, then a second texture which filsl up or depletes based on a variable.

The problem arises in that the first bar works completely. However when a second bar was added with its own textures, Unity begins to fill up with hundreds of these:

null texture passed to GUI.DrawTexture UnityEngine.GUI:DrawTexture(Rect, Texture) Controller:OnGUI() (at Assets/Scripts/Controller.js:121)

The problem I do not understand is that I am creating the Texture2D area for these 2 new textures exactly how I am doing it for the first 2, am filling the variable slot on the inspector with the files, but they will not load. If I simply use the original 2 texture2D textures in place of the new variables, everything works fine.

So, why am I unable to load 4 Texture2D textures? Why do only the first 2 work? I'll paste the code I think matters (omitting the rest of the script), but can post more if needed (please note the ones labeled "progress bar" work, while everything labeled the GCD bar works code-wise but is not loading the textures):

 //GCD Bar
 var gcdProgress : float = 0.0;
 var bar02 : Texture2D;
 var bar03 : Texture2D;
 
 //Progress bar
 var progress : float = 0.0;
 var progressBarEmpty : Texture2D;
 var progressBarFull : Texture2D;
 
 //Function Update isn't as important for this question, just showing where the progress bars are being updated.
 function Update () {
     //Progress Bar
     progress = castbarPub * 0.01;
     
     //GCD Bar
     gcdProgress = globalCoolDownPub * 2;
 }
 
 
 function OnGUI(){
     //GCD Bar
     GUI.DrawTexture(Rect(500, 496, 200, 4), bar02);
     GUI.DrawTexture(Rect(500, 496, 200 * Mathf.Clamp01(gcdProgress), 4), bar03);
 
     //Progress bar
     GUI.DrawTexture(Rect(500, 500, 200, 40), progressBarEmpty);
     GUI.DrawTexture(Rect(500, 500, 200 * Mathf.Clamp01(progress), 40), progressBarFull);
 }

There's obviously a lot of other code omitted but none of it should be interacting with this other than to adjust the variables being used for the progress. I can post it all if need be, however.

Further info would be that the textures that do work are named "BarEmpty" and "BarFull". They are both saved as .tiff and are 200x40 in size. The files that have not worked have been 200x10 and 200x4, and have been named a few different things (I don't think the texture name should matter as long as it has been dragged into the appropriate variable slot on the script?). This is all in Unity Free 4.1.3

Any help with figuring this out is greatly appreciated, or even a nudge in the right direction if someone already asked this question. All I could find myself were people trying to use the function in function Update or just incorrect code, not a problem with code working but these textures not loading. Also if this is not formatted in a way that people tend to expect or need, please let me know.

EDIT: Holy formatting batman.

EDIT 2: Unity Version is 4.1.3 Free.

Edit 3: Here is the full script named "Controller" (There are some variables that probably don't need to exist and need cleaning up, but none should be initializing incorrectly):

 #pragma strict
 
 //Gobal Cooldown
 static var globalCoolDownPub : float = 0.0;
 static var gcdPub = false;
 static var canDragPub = false;
 
 //Tells that the autocast is currently functioning
 static var autoCastPub = false;
 
 //Whether the player can cast or not?
 static var canCastPub : boolean = true;
 
 static var isMouseClickedPub : boolean = false;
 
 //Tells when the cast has ended and a new cast can be started.
 static var castOverPub : boolean = true;
 
 //Is there currently something being cast
 static var castInProgressPub : boolean = false;
 
 //Which bucket is currently being targeted*
 static var targetBucketPub : float = 0.0;
 
 //Which ability is currently highlighted*
 static var currentAbilityPub : float = 2.0;
 
 //CastBar*
 static var castbarPub : float = 0.0;
 
 //Tells the game to reset the cast bar*
 static var castResetPub : boolean = false;
 
 //Cast Speeds
 static var medCastPub : float = 0.0;
 static var bigCastPub : float = 0.0;
 static var aoeCastPub : float = 0.0;
 
 var medCastSpeed : float = 0.0;
 var bigCastSpeed : float= 0.0;
 var aoeCastSpeed : float = 0.0;
 
 //GCD Bar
 var gcdProgress : float = 0.0;
 var bar02 : Texture2D;
 var bar03 : Texture2D;
 
 //Progress bar
 var progress : float = 0.0;
 var progressBarEmpty : Texture2D;
 var progressBarFull : Texture2D;
 
 function Start () {
     //Set public vars to private var values
     medCastPub = medCastSpeed;
     bigCastPub = bigCastSpeed;
     aoeCastPub = aoeCastSpeed;
 
 }
 
 function Update () {
     //GCD
     if(globalCoolDownPub <= 0){
         globalCoolDownPub = 0;
         gcdPub = false;
     }
     if(globalCoolDownPub != 0){
         globalCoolDownPub -= 1 * Time.deltaTime;
     }
     //Progress Bar
     progress = castbarPub * 0.01;
     
     //GCD Bar
     gcdProgress = globalCoolDownPub * 2;
     
     //Changes current ability
     if(Input.GetKey(KeyCode.Alpha1) && castInProgressPub == false){
         currentAbilityPub = 1;
     }
     if(Input.GetKey(KeyCode.Alpha2) && castInProgressPub == false){
         currentAbilityPub = 2;
     }
     if(Input.GetKey(KeyCode.Alpha3) && castInProgressPub == false){
         currentAbilityPub = 3;
     }
     if(Input.GetKey(KeyCode.Alpha4) && castInProgressPub == false){
         currentAbilityPub = 4;
     }
     if(Input.GetKey(KeyCode.Alpha5) && castInProgressPub == false){
         currentAbilityPub = 5;
     }
     
     //Reset the cast bar if casting is canceled before 80%
     if(castResetPub == false && castbarPub < 80 || castResetPub == false && castbarPub >= 100){
         castbarPub = 0;
         targetBucketPub = 0;
         castInProgressPub = false;
         canCastPub = true;
     }
     
     //Prevents the cast bar from exceeding 100% and causes it to reset to 0 upon completion
     if(castbarPub >= 100){
         castbarPub = 0;
         targetBucketPub = 0;
         castInProgressPub = false;
         castResetPub = true;
         autoCastPub = false;
     }
     if(castbarPub == 0 && isMouseClickedPub == false){
         canCastPub = true;
     }
 }
 
 function OnGUI(){
     //debug GUI
     GUI.Label(Rect(10,10,200,20), "Castbar: " + castbarPub);
     GUI.Label(Rect(10,30,200,20), "Reset: " + castResetPub);
     GUI.Label(Rect(10,50,200,20), "Ability: " + currentAbilityPub);
     GUI.Label(Rect(10,70,200,20), "Target: " + targetBucketPub);
     GUI.Label(Rect(10,90,200,20), "GCD: " + globalCoolDownPub);
 
     //GCD Bar
     GUI.DrawTexture(Rect(500, 496, 200, 4), progressBarEmpty);
     GUI.DrawTexture(Rect(500, 496, 200 * Mathf.Clamp01(gcdProgress), 4), progressBarFull);
 
     //Progress bar
     GUI.DrawTexture(Rect(500, 500, 200, 40), progressBarEmpty);
     GUI.DrawTexture(Rect(500, 500, 200 * Mathf.Clamp01(progress), 40), progressBarFull);
 }

And then, if needed, the other script named "BucketScript01" running with it:

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
     if(Controller.castbarPub >= 80 && Controller.castbarPub <= 100 && Controller.targetBucketPub == 1 && Controller.currentAbilityPub == 2){
         Controller.autoCastPub = true;
         Controller.castResetPub = true;
         Controller.canCastPub = false;
         Controller.castbarPub += Controller.medCastPub * Time.deltaTime;
         transform.Rotate(0,0,5);
     }
     if(Controller.castbarPub >= 80 && Controller.castbarPub <= 100 && Controller.targetBucketPub == 1 && Controller.currentAbilityPub == 3){
         Controller.autoCastPub = true;
         Controller.castResetPub = true;
         Controller.canCastPub = false;
         Controller.castbarPub += Controller.bigCastPub * Time.deltaTime;
         transform.Rotate(0,0,2);
     }
 }
 
 function OnMouseDown () {
     //isMouseClicked flags true when the mouse has been clicked and unflags when it is unclicked.
     Controller.isMouseClickedPub = true;
     //Checks if the global cooldown is currently active
     if(Controller.gcdPub == false){
         //If the GCD is inactive, flags that the drag may work
         Controller.canDragPub = true;
     }
     if(Controller.globalCoolDownPub == 0 && Controller.castInProgressPub == false){
         Controller.globalCoolDownPub = 0.5;
         Controller.gcdPub = true;
     }
 }
 
 function OnMouseDrag () {
     if(Controller.canCastPub == true && Controller.canDragPub == true && Controller.currentAbilityPub == 2 && Controller.autoCastPub == false){
         if(Controller.castbarPub < 80){
             //Sets the target bucket to this bucket
             Controller.targetBucketPub = 1;
             //ResetPub tells the cast bar to reset to 0 if the mouse is let up before the casting finishes
             Controller.castResetPub = true;
             //CastInProgress flags true if a cas is in progress. Unused currently.
             Controller.castInProgressPub = true;
             //Counts up the cast bar
             Controller.castbarPub += Controller.medCastPub * Time.deltaTime;
             //Rotates the object to show it is working.
             transform.Rotate(0,0,5);
         }
     }
     if(Controller.canCastPub == true && Controller.canDragPub == true && Controller.currentAbilityPub == 3 && Controller.autoCastPub == false){
         if(Controller.castbarPub < 80){
             Controller.targetBucketPub = 1;
             Controller.castResetPub = true;
             Controller.castInProgressPub = true;
             Controller.castbarPub += Controller.bigCastPub * Time.deltaTime;
             transform.Rotate(0,0,2);
         }
     }
 }
 
 function OnMouseUp () {
     //isMouseClicked flags true when the mouse has been clicked and unflags when it is unclicked.
     Controller.isMouseClickedPub = false;
     //ResetPub tells the cast bar to reset to 0 if the mouse is let up before the casting finishes
     Controller.castResetPub = false;
     Controller.canDragPub = false;
 }
Comment
Bunny83

People who like this

1 Show 2
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 robertbu · Jun 02, 2013 at 01:40 AM 1
Share

I cannot reproduce your problem. I did not see anything that, if the code was properly initialized, would cause a null texture error. So I dropped the script into Unity, fixed castbarPub and globalCoolDownPub, assigned textures and hit play. And no null textures errors. Either the error is elsewhere and you need to include more code, or you have a problem with how you initialized the textures.

avatar image Ria Ayo · Jun 02, 2013 at 02:05 AM 0
Share

I went ahead and edited in the full script along with the other script running into the original post at the bottom. I'm going to try creating a new project and pasting the code into that to see if there is just some unseen issue with the current project... I have no idea what it could be, though.

But as far as the textures go, the code is as you saw it. I declared the variable, then in the editor I dragged and dropped the texture files into the variable slots on the script. For whatever reason those original 2 work fine, but any others I try to load in just won't go.

EDIT: I've created a new project, and now the new textures do load. However now the green bar is now showing up. It does not throw any sort of error like it was before, the green progress bar simply isn't appearing.

 GUI.DrawTexture(Rect(500, 500, 200 * Mathf.Clamp01(progress), 40), progressBarFull);

What is in this line specifically doesn't seem to be showing. I've changed the texture variable being used and it still doesn't show up. Again, it is not throwing a Null pass, it just simply is not visible. It is also not behind the empty bar, as I have disabled it and it doesn't show up even then. Same code, copied right in... is this some sort of problem with my engine's install?

EDIT2: Bunny83 pointed out my error below. But thank you so much for helping out none the less.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Jun 02, 2013 at 03:13 AM

From your comments I guess you just assigned the textures to the "default references" of the script itself but not on your actual instance. The default references are only used when the script component is added to a GameObject to initialize the variables. I guess that you already have an instance of your script in your scene or on a prefab and there you don't have the textures assigned since you set the default references afterwards.

Such things can easily be verified. When you test inside the editor, just select your GameObject in the scene that has the script attached. Now you can see if the textures are assigned or not.

The default references are quite handy if you need to assign a lot of textures / sounds / ... but use them with caution. Default references are also included into a build, even when they aren't used. If you don't create a lot instances of your script in the editor you should avoid using default references. Just assign the textures directly on your object.

Comment
robertbu

People who like this

1 Show 2 · 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 Ria Ayo · Jun 02, 2013 at 03:22 AM 0
Share

This seems like something I should have noticed myself, however the script was on an empty game object so I was not clicking on that object itself very often at all. This absolutely is the issue, though I had no idea that updating the script itself would not update any instances of itself on an object.

Thank you so much for pointing this out, and thanks to Robertbu for trying to reproduce the problem for me earlier as well. I will keep in mind that Unity handles things in this manner in the future.

avatar image $$anonymous$$ · May 02, 2014 at 06:45 PM 0
Share

Thank you a lot.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Better performance? GUITextures or Texture2D drawn in function OnGUI? 0 Answers

iOS OnGUI vs Update for color picker 2 Answers

Changing Texture2D at runtime, but it doesent work? 0 Answers

GUI.DrawTexture() into a Layer? 1 Answer

Making texture cover whole button 1 Answer


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