StackOverflowException: The requested operation caused a stack overflow. (Переполнение стака в Unity)

Всем здравствуйте, Ошибка такая:

StackOverflowException: The requested operation caused a stack overflow.

Смотрел на форумах способы решения, но никак не могу соотнести с моим случаем. Указывает место ошибки на

[HideInInspector] public BrushPainting bp = new BrushPainting();

[SerializeField] private Camera MainCamera;

[SerializeField] private Collider PZCollider;

[SerializeField] private Transform LastTouchCoordsObj;


[HideInInspector] public int XPZResolution = 500;
[HideInInspector] public int YPZResolution;
[HideInInspector] public Texture2D PaintingZoneTexture;


[HideInInspector] public BrushPainting bp = new BrushPainting();
PaintingZoneLoader pzl = new PaintingZoneLoader();
[HideInInspector] public ScriptPainting sp = new ScriptPainting();

public class BrushPainting : Painting
{
public int BrushSize = 1;

private Color BrushColor = Color.red;

public int XOldHitCoord;
public int YOldHitCoord;

public bool FirstTouch = true;

public bool PaintingProcess = false;
public void ToPaint()
{
Ray ray = MainCamera.ScreenPointToRay(Input.mousePosition);

RaycastHit hit;

if (PZCollider.Raycast(ray, out hit, 10f))
{
int XHitCoord = (int)(hit.textureCoord.x * XPZResolution);
int YHitCoord = (int)(hit.textureCoord.y * YPZResolution);

if (XHitCoord > XOldHitCoord)
{
if (PaintingProcess == false)
{
XHitCoord = XOldHitCoord + 1 + BrushSize * 2;
}

PaintingProcess = true;

if ((Mathf.Abs(XHitCoord - XOldHitCoord) >= 2 * BrushSize || Mathf.Abs(YHitCoord - YOldHitCoord) >= 2 * BrushSize) && !FirstTouch)
{
FillingSpaces(XOldHitCoord, YOldHitCoord, XHitCoord, YHitCoord);
}

SetBrushPrint(XHitCoord, YHitCoord);

XOldHitCoord = XHitCoord;
YOldHitCoord = YHitCoord;

PaintingZoneTexture.Apply();
}
else
{
PaintingProcess = false;
}
}
}
public void FillingSpaces(int XOldCoordinate, int YOldCoordinate, int XNewCoordinate, int YNewCoordinate)
{
float CoordDeltaX = XNewCoordinate - XOldCoordinate;
float CoordDeltaY = YNewCoordinate - YOldCoordinate;

int DeltaX = (int)Mathf.Abs(CoordDeltaX / BrushSize);
int DeltaY = (int)Mathf.Abs(CoordDeltaY / BrushSize);

int BrushPosX;
int BrushPosY;

if (Mathf.Abs(CoordDeltaX) >= Mathf.Abs(CoordDeltaY))
{
for (int i = 0; i < DeltaX; i++)
{
BrushPosX = XOldCoordinate + BrushSize * i * (CoordDeltaX >= 0 ? 1 : -1);

BrushPosY = (int)(YOldCoordinate + (CoordDeltaY / CoordDeltaX) * BrushSize * i * (CoordDeltaX >= 0 ? 1 : -1));

SetBrushPrint(BrushPosX, BrushPosY);
}
}
else
{
for (int i = 0; i < DeltaY; i++)
{
BrushPosY = YOldCoordinate + BrushSize * i * (CoordDeltaY >= 0 ? 1 : -1);

BrushPosX = (int)(XOldCoordinate + (CoordDeltaX / CoordDeltaY) * BrushSize * i * (CoordDeltaY >= 0 ? 1 : -1));

SetBrushPrint(BrushPosX, BrushPosY);
}
}
}
public void SetBrushPrint(int XCoordinate, int YCoordinate)
{
for (int x = -BrushSize; x <= BrushSize; x++)
{
for (int y = -BrushSize; y <= BrushSize; y++)
{
PaintingZoneTexture.SetPixel(XCoordinate + x, YCoordinate + y, BrushColor);
}
}
}
public void TeleportingObgInPZSpace(Transform Object, int XPZCoord, int YPZCoors)
{
Object.position = new Vector2(((float)XPZCoord - XPZResolution / 2) * Camera.main.aspect / XPZResolution, ((float)YPZCoors - YPZResolution / 2) / YPZResolution);
}
}
private class PaintingZoneLoader : Painting
{
private FilterMode BlurMode = FilterMode.Point;
public void Loading()
{
YPZResolution = (int)(XPZResolution / Camera.main.aspect);

PaintingZoneTexture = new Texture2D(XPZResolution, YPZResolution);

Renderer r = GetComponent<Renderer>();

r.material.mainTexture = PaintingZoneTexture;

PaintingZoneTexture.wrapMode = TextureWrapMode.Clamp;

PaintingZoneTexture.filterMode = BlurMode;


Transform tr = GetComponent<Transform>();

tr.localScale = new Vector3(Camera.main.aspect, 1, 1);
}
}
public class ScriptPainting : Painting
{
public int YAxisOffsetInCells = 5;
public int CellSize = 10;
public void CellsDrawing()
{
for (int x = 0; x < XPZResolution; x++)
{
for (int y = 0; y < YPZResolution; y++)
{
if (x % CellSize == 0 || y % CellSize == 0)
{
PaintingZoneTexture.SetPixel(x, y, Color.black);
}
else
{
PaintingZoneTexture.SetPixel(x, y, Color.white);
}
}
}

PaintingZoneTexture.Apply();
}
public void AxisesDrawing()
{
for (int y = 0; y < YPZResolution; y++)
{
for (int i = 0; i < 2; i++)
{
PaintingZoneTexture.SetPixel(YAxisOffsetInCells * CellSize + i, y, Color.black);
}
}

for (int x = 0; x < XPZResolution; x++)
{
for (int i = 0; i < 2; i++)
{
PaintingZoneTexture.SetPixel(x, (int)(YPZResolution / CellSize / 2) * CellSize + i, Color.black);
}
}

PaintingZoneTexture.Apply();
}
public void DataReset()
{
bp.XOldHitCoord = YAxisOffsetInCells * CellSize;

bp.YOldHitCoord = (int)(YPZResolution / CellSize / 2) * CellSize;
}
}

private void Awake()
{
pzl.Loading();
}
private void Start()
{

}
private void Update()
{
if (Input.GetMouseButton(0))
{
bp.ToPaint();

bp.FirstTouch = false;
}
else
{
bp.FirstTouch = true;

bp.PaintingProcess = false;
}

if (Input.GetMouseButtonUp(0))
{
bp.TeleportingObgInPZSpace(LastTouchCoordsObj, bp.XOldHitCoord, bp.YOldHitCoord);
}
}```

Ответы (0 шт):