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 remmbermytitans · Oct 01, 2012 at 03:22 AM · texture2dnullreferenceexceptionmaterialstextures

Problems setting up a Texture2D

I am trying to set up a Texture2D using either the material/texture of a GameObject or by loading a material/texture from my Resources folder, but keep getting the following error:

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

Here is the code I've been TRYING to use with no success:

 **First attempt:**
 Texture2D tex = new Texture2D(200,200);
 tex = Resources.Load("imageCamera") as Texture2D;
 byte[] bytes = tex.EncodeToPNG();
 Destroy(tex);
 File.WriteAllBytes(Application.persistentDataPath + "/SavedS2creen.png", bytes);
 
 **Second attempt:**
 Texture2D tex2 = GameObject.Find("CapturedPicture").renderer.material.GetTexture("_MainTex") as Texture2D;
 byte[] bytes = tex2.EncodeToPNG();

If anyone has any idea how to help me out, I would really appreciate it.

EDIT: I know that for sure the material/texture that I'm trying to load is not null. If you look at my "second attempt", you'll see that I have a GameObject called CapturedPicture. That has the material in question that I want to assign to a Texture2D.

Comment
Add comment · Show 4
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 karl_ · Oct 01, 2012 at 04:44 AM 0
Share

Does the texture you're attempting to encode to png have the IsReadable flag set?

avatar image remmbermytitans · Oct 01, 2012 at 04:49 AM 0
Share

No, I wasn't even aware that was an option. Should I set "IsReadable"?

avatar image karl_ · Oct 01, 2012 at 04:53 AM 0
Share

Yes, EncodeToPNG will not work unless it is readable. Edit: posted this as an answer with code example.

avatar image flamy · Oct 01, 2012 at 04:59 AM 0
Share

added to karl_ 's comment, is the size of the texture you are trying to load same as the one you are using in code (200x200)

One more thing in which line you are getting null reference.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by karl_ · Oct 01, 2012 at 04:59 AM

For EncodeToPNG to work, the texture must first have it's IsReadable flag set to true.

 string path AssetDatabase.GetAssetPath( (Texture2D)tex );
 TextureImporter ti = TextureImporter.GetAtPath( path ) as TextureImporter;
 ti.isReadable = true;
 AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
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 remmbermytitans · Oct 01, 2012 at 05:05 AM 0
Share

Using the code I provided above, could you show me exactly how to add your code and my code to make it work? I'm a little lost. Thank you so much for helping.

avatar image karl_ · Oct 01, 2012 at 05:11 AM 0
Share

Just place the chunk of code I provided immediately following the line `tex = Resources.Load()`

avatar image remmbermytitans · Oct 01, 2012 at 05:11 AM 0
Share

Also, using the code above doesn't work. I get three errors, AssetDatabase, TextureImporter does not exist in the current context. EDIT: Never $$anonymous$$d about this comment, I had to import something.

avatar image karl_ · Oct 01, 2012 at 05:16 AM 0
Share

Right, AssetDatabase is an Editor only class.

avatar image
0

Answer by remmbermytitans · Oct 01, 2012 at 05:23 AM

I added the code, so that it now looks like this:

 Texture2D tex = new Texture2D(200, 200);
 tex = Resources.Load("imageCamera") as Texture2D;
         
 string path = AssetDatabase.GetAssetPath( (Texture2D)tex );
 TextureImporter ti = TextureImporter.GetAtPath( path ) as TextureImporter;
 ti.isReadable = true; //nullRef
 AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
         
 byte[] bytes = tex.EncodeToPNG();
 Destroy(tex);
 File.WriteAllBytes(Application.persistentDataPath + "/SavedS2creen.png", bytes);

But now I'm getting a NullReferenceException at ti.isReadable = true;

Any thoughts?

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 karl_ · Oct 01, 2012 at 05:30 AM 0
Share

Are you running this code in editor or at runtime?

avatar image remmbermytitans · Oct 01, 2012 at 05:33 AM 0
Share

This is happening at runtime. $$anonymous$$ore specifically, when a user taps on a specific gameobject. Again, thank you for taking the time to assist me. I am truly stumped.

avatar image karl_ · Oct 01, 2012 at 05:36 AM 0
Share

Ah, well this won't help you then. As mentioned earlier, AssetDatabase is an Editor class only, meaning it won't work at runtime. You can still use the Inspector to manually set IsReadable for the texture though. If you're just looking to create a screenshot, try out this script on the Unify wiki: http://wiki.unity3d.com/index.php?title=TakeScreenshot

avatar image remmbermytitans · Oct 01, 2012 at 05:37 AM 0
Share

I've seen the "TakeScreenshot" threads, but I'm looking more specifically for a screenshot of a single GameObject. If you have any ideas on how to do that, that would be great. If not, then I again thank you for your time.

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

12 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

Related Questions

Texturing a large surface 2 Answers

Checking for null against Texture2D 0 Answers

Assigning textures in project panel to materials dynamically or via editor scripts 1 Answer

set minimum texture resolution? 2 Answers

create different materials using the same texture 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