"Could not create sprite" exception - copying and manipulating texture from SpriteAtlas

I have a bunch of 8bit car sprites, with black outlines and white paint. I am using the method called CopyTexture2D() from post #8 in this thread to change the color of the car to a random RGB value, generating a bunch of traffic.

That solution works brilliantly when I have a folder full of individual sprites. However, when pulling a sprite from a SpriteAtlas (and also when loading sprites directly from a multiple sprite sheet into Sprite) it errors. The following code:

Sprite.Create(carTexture, carSprite.rect, new Vector2(0f, 1f));

returns the following error:

ArgumentException: Could not create sprite (0.000000, 200.000000, 101.000000, 50.000000) from a 101x50 texture.

0, 200 is the position of the selected sprite within the sprite atlas. I can avoid the error by changing it to -

Sprite.Create(carTexture, new Rect(0f, 0f, 101f, 50f), new Vector2(0f, 1f));
  • but that pulls the sprite from position 0, 0 in the sprite atlas, rather than the sprite I originally loaded.

The full code (related to this problem, anyway) is-

public Transform GenerateCar(Vector2 position, Quaternion rotation)
    string spriteName = "8bit-Car-Full-Sprite-Sheet_" + carNo.ToString();
    if (carNo > 49)
        carNo = 0;
    Sprite carSprite = sa.GetSprite(spriteName);
    Transform car = Instantiate(prefab, position, rotation);
    // here is the call (CopyTexture2D() from the referenced post, here renamed to PaintCar())
    Texture2D carTexture = PaintCar(carSprite.texture);
    // here is the error
    Sprite sprite = Sprite.Create(carTexture, carSprite.rect, new Vector2(0f, 1f));
    car.GetComponent<SpriteRenderer>().sprite = sprite;
    return car;


private Texture2D PaintCar(Texture2D copy)
    // pick a random RGB color
    byte colorR = (byte)UnityEngine.Random.Range(0, 255);
    byte colorG = (byte)UnityEngine.Random.Range(0, 255);
    byte colorB = (byte)UnityEngine.Random.Range(0, 255);
    Texture2D texture = new Texture2D(101, 50, TextureFormat.ARGB32, false);
    texture.filterMode = FilterMode.Point;
    texture.wrapMode = TextureWrapMode.Clamp;
    // loop through each row of pixels
    int y = 0;
    while (y < texture.height)
        // loop through each pixel in the row
        int x = 0;
        while (x < texture.width)
            if (copy.GetPixel(x, y) == Color.white )
                texture.SetPixel(x, y, new Color32(colorR, colorG, colorB, 255));
                texture.SetPixel(x, y, copy.GetPixel(x, y));
    return texture;

Hopefully, that’s enough info to help get to the bottom of this. Let me know if I am missing something you need. Thanks in advance!

After giving up and importing individual sprites, I found that I had to delete the now unused SpriteAtlas in the Sprites folder. The SpriteAtlas was still being used behind the scenes when referencing the sprite I had manually loaded with Resources.Load(“Sprites/Car01”). The Texture2D was later being swapped with the sprite in that position in the SpriteAtlas.

Lessen learned, I guess.