Paint a sprite

I am trying to paint an image in real time. For that I have an Image in my scene and a script.

In my script I have and Image, that corresponds to my scene Image, and a Texture2D that I create in my script:

Image img;

Texture2D txt;

void Start()
{
img = GameObject.Find(“Button”).GetComponent();
txt = new Texture2D((int)img.rectTransform.sizeDelta.x, (int)img.rectTransform.sizeDelta.y);
}

Then, in my update I check if the mouse is being pressed, if so, I paint a dot in my texture in the mouse position, I create a sprite from that Texture2D, and finally I change my Image sprite to the one created by me, like this:

void Update()
{
if (mouseDown)
{
txt = Circle(txt, Input.mousePosition.x, Input.mousePosition.y, 20, Color.green);

Sprite s = Sprite.Create(txt, new Rect(0, 0, (int)img.rectTransform.sizeDelta.x - 2, (int)img.rectTransform.sizeDelta.y - 2), new Vector2(0.5f, 0.5f));

img.sprite = s;
}
}

The function circle correcly paints the texture, I encoded the texture to jpg and save it as a file to check its content, everything was fine, so the problem is in the next two lines of code… I have no ideia what might be worng, probably the sprite creation? Anyone knows what I am doing wrong?

Here is the Circle function if anyone wants to try to reproduce the error:

public Texture2D Circle(Texture2D tex, float cx, float cy, float r, Color col)
{
//byte[ ] or = tex.EncodeToJPG();
//File.WriteAllBytes(@“c:\test\or.jpg”, or);

float x, y, px, nx, py, ny, d;

for (x = 0; x <= r; x++)
{
d = (int)Mathf.Ceil(Mathf.Sqrt(r * r - x * x));
for (y = 0; y <= d; y++)
{
px = cx + x;
nx = cx - x;
py = cy + y;
ny = cy - y;

tex.SetPixel((int)px, (int)py, col);
tex.SetPixel((int)nx, (int)py, col);

tex.SetPixel((int)px, (int)ny, col);
tex.SetPixel((int)nx, (int)ny, col);
}
}

//byte[ ] fe = tex.EncodeToJPG();
//File.WriteAllBytes(@“c:\test\fe.jpg”, fe);

return tex;
}

I can’t see any mistakes… either because I’m too tired right now (i can’t sleep although I’ve taken a zolpidem er). Gonna look at it tomorrow, or maybe someone else would see something…