- Home /
NullReferenceException on GetPixels for Texture
I'm new to Unity. What I want to do here is create a single runtime sprite consisting of a combination of smaller sprites. I want to create a single, larger sprite, with a single renderer, single collision model, etc.
So the code below blows up at line 32 when I try and copy the pixels from an intermediate texture. I get the NullReferenceException. Any ideas why?
using UnityEngine;
using System.Collections;
public class CreateObstacles : MonoBehaviour {
// private GameObject obstacle;
private GameObject player;
// Use this for initialization
void Start () {
float xlength=2000;
//Get Sprites into memory
Sprite[] sprites=Resources.LoadAll<UnityEngine.Sprite>("Sprites");
//Add renderer to object
SpriteRenderer spriteRenders= gameObject.AddComponent<SpriteRenderer>();
Texture2D t = new Texture2D(100,70); //Target new Texture which will eventually be a sprite
int x = Mathf.FloorToInt(sprites[1].textureRect.x);
int y = Mathf.FloorToInt(sprites[1].textureRect.y);
int width = Mathf.FloorToInt(sprites[1].textureRect.width);
int height = Mathf.FloorToInt(sprites[1].textureRect.height);
Texture2D tt =new Texture2D(width,height); // create texture for sprite1
tt=sprites[1].texture; //copy texture from sprite1
//BLOWS UP HERE
Color[] pix=tt.GetPixels(x,y,width,height); //get color array
//t.SetPixels(x,y,width,height,pix);
Sprite newsprite=Sprite.Create (t,new Rect(0f,0f,100f,70f),new Vector2(0f,0f),70f);
looks like sprite[1].texture is null add this line at line 28
Debug.Log(sprites[1].texture);
First, is it normal that you read sprites[1] and not sprites[0] ?
Then, line 29 you write :
Texture2D tt =new Texture2D(width,height);
and just after that you do :
tt=sprites[1].texture;
So you instantiate a new texture and you forget it just after ?
sprites[0] thru sprites[19] is not null (loaded from resources), A spritemap actually in Unity 4.3. What I attempted with tt=sprites[1].texture; was to assign a texture to what I thought was presumably an unadorned texture2d from the naked constructor above. That works, and if left out gives a blank/white texture. the problem is copying the pixels to the Color[] array... I eventually want to copy a random number of sprites into a new texture and then use that to create a new larger sprite. not working though!
So ok for sprites[1], but
Texture2D tt =new Texture2D(width,height);
tt=sprites[1].texture;
is useless.
It's like :
int x = -3;
x=2;
The first line is not used.
So if sprites[1].texture is null, even if you did Texture2D tt =new Texture2D(width,height) before, tt will be null.
$$anonymous$$ira..understood.. the original code was Texture2D tt=sprites[1].texture; you are right. It seems the problem is that the underlying sprite resource is a sprite sheet, and it seems there's problems getting a reference to it.
Answer by djhyman · Nov 25, 2013 at 09:27 AM
Progress Made!
The problem is the sprite sheet was not marked to be read in Advanced Import settings, as in the following in the inspector:
why would that cause a NullReferenceException ? Usually you get a Unity Exception and it will tell you that you need to enable read/write? Did a read /Write come up in the warning sections?
the only possible way i can see why that would cause a null reference is that the sprite class didn't assign the texture because it can't read it. but then it should of giving you a Read/Write Error.
Anyway glad you got it sorted.