Sprite.Create() only works the first time and eventually causes "Invalid AABB" errors.

So, I’m trying to have some images on a UI change based on data about levels in my game, and this is my code for one of those things:

contentPanel.transform.GetChild(0).gameObject.GetComponent() .sprite = Sprite.Create(lvl.ImageID, contentPanel.transform .GetChild(0).gameObject.GetComponent<Image>().sprite.textureRect, contentPanel.transform.GetChild(0).gameObject.GetComponent<Image>() .sprite.pivot);

“contentPanel.transform.GetChild(0).gameObject” refers to an image that is the first child of a panel on the canvas. lvl.ImageID is the thumbnail for a level.

The code runs whenever the player hits a button to open the canvas (it’s actually always open, it’s just hidden behind everything else when it’s “closed”). The first time, it runs well, but the second time, the image I’m trying to load is drawn off-center even though the bounding box and pivot are in the right spot and the third time and afterward, the image doesn’t draw at all (even though the bounding box and pivot are still right where they should be) and I get an error message that says, “Invalid AABB in AABB.” The call stack never mentions my code. What is going on?

UPDATE: After more testing, I found that reloading the scene “resets” the bug.

ANOTHER UPDATE: I changed the pivot parameter in one of the Sprite.Create() functions to new Vector2(0.5f, 0.5f) and the problem got fixed for the thing that that function was helping to draw. (For anyone reading this in the future, I think that was only part of the fix, read the answers and comments.)

Invalid AABB in AABB usually means that the texture you want to create a sprite of is not set to readable. I’m the inspector, in the texture import settings, make sure that “Read/Write enabled” is set to true.
Also for the textureRect, you don’t want to use the previous one, but the size of the texture. So, instead of contentPanel.transform .GetChild(0).gameObject.GetComponent<Image>().sprite.textureRec use new Rect(0, 0, lvl.ImageID.width, lvl.ImageID.height , if I understand correctly that lvl.ImageID is your texture
What I suggested only works with single textures, not packed textures

@sheepmccree My texture is part of a multiple sprite image that isn’t using an atlas. Does that matter?