i'm trying to make the packed atlas and use the reacts to get the width and height then apply it to the material how do you do it

keep getting error Array size must be at least width*height? how do you fix it Runtime error

    public Texture2D[] atlasTextures;
    public Rect[] rects;
	public Texture2D atla;

    void Start() {
        Texture2D atlas = new Texture2D(8192, 8192);
        rects = atlas.PackTextures(atlasTextures, 2, 8192);
		
		atla = atlas;
		
		
		StartCoroutine(Example());
    }
	
	 IEnumerator Example() {	
		for(int i=0 ; i<atlasTextures.Length; i++){
		int x = Mathf.FloorToInt(rects*.x);*

int y = Mathf.FloorToInt(rects*.x);*
int width = Mathf.FloorToInt(rects*.width);*
int height = Mathf.FloorToInt(rects*.height);*

Color[] pix = atla.GetPixels(x, y, width, height);
Texture2D destTex = new Texture2D(width, height,TextureFormat.ARGB32,false);
* destTex = atla;*
destTex.SetPixels(pix);
destTex.Apply();
renderer.material.mainTexture = destTex;
* yield return new WaitForSeconds(0.5f);*
* }*
}

I don’t have the time to test this, but I think this might be your problem: rectangles returned by PackTextures() are relative to the size of texture. That means that all it’s values are between 0.0f and 1.0f. If size of your sub-texture is smaller then size of the atlas (usually it is), the value is less then 1.0f and by flooring it you get 0. So you probably create empty texture.

You need to multiply all rectangle values (x, y, width, height) by size of the atlas texture.