Texture rectangle is out of bounds

Why this script get the error “Texture rectangle is out of bounds (512 + 1 > 512)” I am using this script in a Terrain to create a crater on terrain in runtime.

private TerrainData tData;
public float[,,] saved;

private int heights;

public Texture2D cratertex;
private int xRes;
private int yRes;


private int layers;

private Color[] craterData;

[Range(0.009f, 0.0001f)]
public float SmoothingValue;

// Start is called before the first frame update
void Start()
{
    tData = Terrain.activeTerrain.terrainData;
    xRes = tData.alphamapWidth;
    yRes = tData.alphamapHeight;
  
 
    craterData = cratertex.GetPixels();
   
}

// Update is called once per frame
void Update()
{

     saved = tData.GetAlphamaps(0, 0, xRes, yRes);
    
    if (Input.GetMouseButtonDown(0))
    {
       
       float g = Mathf.Lerp(0, xRes, Mathf.InverseLerp(0, tData.size.x, MousePosTerrain.mousepos.x)) - this.transform.position.x;
        float b = Mathf.Lerp(0, yRes, Mathf.InverseLerp(0, tData.size.z, MousePosTerrain.mousepos.z)) - this.transform.position.z;
        g = Mathf.Clamp(g, cratertex.width / 2, xRes - cratertex.width / 2);
        b = Mathf.Clamp(b, cratertex.height / 2, yRes - cratertex.height / 2);

      

        float xFloat = Mathf.Round(g);
        float zFloat = Mathf.Round(b);
        int xInt = (int)xFloat;
        int zInt = (int)zFloat;

        

        var area = tData.GetAlphamaps(xRes - cratertex.width / 2 , yRes - cratertex.height / 2, cratertex.width, cratertex.height);
       
      //  Debug.Log(xRes);
        for (int i = 0; i < cratertex.height; i++)
        {
            for (int j = 0; j < cratertex.width; j++)
            {
                for (int l = 0; l < layers; l++)
                {
                    if (l == 1)
                    {
                        area[i, j, l] += craterData[i * cratertex.width + j].a;
                    }
                    else
                    {
                        area[i, j, l] -= craterData[i * cratertex.width + j].a;
                    }

                }

            }

      
        }
   
        tData.SetAlphamaps(xInt - cratertex.width / 2, zInt - cratertex.height / 2,area);

      
   

    }

}

}

Ok, I found a solution if anyone needs, thanks.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TerrainMorpher : MonoBehaviour
{

private TerrainData tData;
public float[,] saved;
public float[,,] alphamapSave;

[SerializeField]
int layers;

// private float[,] heights;


public Texture2D cratertex;
private int xRes;
private int yRes;
private int xResL;
private int yResL;

private Color[] craterData;


[Range(1.0f, 0.0001f)]
public float SmoothingValue;



void Start()
{
    

    tData = Terrain.activeTerrain.terrainData;
    xRes = tData.heightmapWidth;
    yRes = tData.heightmapHeight;
    xResL = tData.alphamapWidth -1;
    yResL = tData.alphamapHeight - 1;

   // tData.GetAlphamaps(0, 0, xRes, yRes);
   craterData = cratertex.GetPixels();

   // layers = tData.alphamapLayers;
}


// Update is called once per frame
void Update()
{
    tData.RefreshPrototypes();
   
     

    saved = tData.GetHeights(0, 0, xRes, yRes); //Save modifications to terrain data
    alphamapSave = tData.GetAlphamaps(0, 0, xResL, yResL);

    if (Input.GetMouseButtonDown(0))
    {
      
        float x = Mathf.Lerp(0, xRes, Mathf.InverseLerp(0, tData.size.x, MousePosTerrain.mousepos.x)) - this.transform.position.x;
        float z = Mathf.Lerp(0, xRes, Mathf.InverseLerp(0, tData.size.z, MousePosTerrain.mousepos.z)) - this.transform.position.z;
        x = Mathf.Clamp(x, cratertex.width / 2, xRes - cratertex.width / 2);
        z = Mathf.Clamp(z, cratertex.height / 2, yRes - cratertex.height / 2);

        float xFloat = Mathf.Round(x);
        float zFloat = Mathf.Round(z);
        int xInt = (int)xFloat;
        int zInt = (int)zFloat;

       // Debug.Log(xInt);
       // Debug.Log(zInt);
        float[,] areaT = tData.GetHeights(xInt - (cratertex.width / 2), zInt - cratertex.height / 2, cratertex.width, cratertex.height);
        float[,,] area = tData.GetAlphamaps(xResL - (cratertex.width / 2), yResL - cratertex.height / 2, cratertex.width, cratertex.height);
        for (int i = 0; i < cratertex.height; i++)
        {
            for (int j = 0; j < cratertex.width; j++)
            {

                areaT[i, j] = areaT[i, j] - craterData[i * cratertex.width + j].a * 0.0004f;
                for (int l = 0; l < layers; l++)
                {
                    if (l == 0)
                    {
                        area[i, j, l] =+ craterData[i * cratertex.width + j].a * 0.0004f;
                    }
                    else
                    {
                        area[i, j, l] =- craterData[i * cratertex.width + j].a * 0.0004f; 
                    }

                }
            }
        }
      
       
        tData.SetHeights(xInt, zInt, areaT);
        tData.SetAlphamaps(xInt - cratertex.width / 2, zInt - cratertex.height / 2, area);
       // Debug.Log(layers);
     
    }
}

}