- Home /
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.
Does the texture you're attempting to encode to png have the IsReadable flag set?
No, I wasn't even aware that was an option. Should I set "IsReadable"?
Yes, EncodeToPNG will not work unless it is readable. Edit: posted this as an answer with code example.
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.
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);
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.
Just place the chunk of code I provided immediately following the line `tex = Resources.Load()`
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.
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?
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.
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
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.