how do I fix this bug in my code "The name 'chessBoard' does not exist in the current context" even though it does exist in the contex?

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

public class pawn_movement : MonoBehaviour
{
    Vector2 startpos;
    private bool isDragging;
    private float moves = 0;
    private float differentmoves;
    private int precol;
    private int prerow;
    private int col;
    private int row;
    public bool Initializedcheck = false;
    public void OnMouseDown()
    {
        differentmoves = 0;
        startpos = transform.position;
        isDragging = true;
    }

    public void OnMouseUp()
    {

        isDragging = false;
        Vector2 pos = transform.position;
        pos.x = Mathf.Round(pos.x);
        pos.y = Mathf.Round(pos.y);
        transform.position = pos;
        if ( transform.position.x < -.5 || transform.position.x > 7.5 || transform.position.y < -.5 || transform.position.y > 7.5)
        {
            transform.position = startpos;
        }
        
        if (moves == 0 && transform.position.y-startpos.y > 0.5 && transform.position.y-startpos.y <= 2.5 && transform.position.x-startpos.x < 0.5 && transform.position.x-startpos.x > -0.5) 
        {
            moves += 1;
            differentmoves = 1;
            if (Initializedcheck == false)
            {
            ChessBoard chessBoard = new ChessBoard();
            precol = (int)Mathf.Round(startpos.x);
            prerow = (int)Mathf.Round(startpos.y);
            col = (int)Mathf.Round(transform.position.x);
            row = (int)Mathf.Round(transform.position.y);
            chessBoard.MovePiece(prerow, precol, row, col);
            Initializedcheck = true;
            }
            else
            {
            precol = (int)Mathf.Round(startpos.x);
            prerow = (int)Mathf.Round(startpos.y);
            col = (int)Mathf.Round(transform.position.x);
            row = (int)Mathf.Round(transform.position.y);
            **chessBoard**.MovePiece(prerow, precol, row, col); 
            }
            
        }
        else if (differentmoves == 0 && transform.position.y-startpos.y > 0.5 && transform.position.y-startpos.y < 1.5 && transform.position.x-startpos.x < 0.5 && transform.position.x-startpos.x > -0.5)
        {
            if (Initializedcheck == false)
            {
            ChessBoard chessBoard = new ChessBoard();
            precol = (int)Mathf.Round(startpos.x);
            prerow = (int)Mathf.Round(startpos.y);
            col = (int)Mathf.Round(transform.position.x);
            row = (int)Mathf.Round(transform.position.y);
            chessBoard.MovePiece(prerow, precol, row, col);
            Initializedcheck = true;
            }
            else
            {
            precol = (int)Mathf.Round(startpos.x);
            prerow = (int)Mathf.Round(startpos.y);
            col = (int)Mathf.Round(transform.position.x);
            row = (int)Mathf.Round(transform.position.y);
            **chessBoard**.MovePiece(prerow, precol, row, col);
            }
        }
        else
        {
            transform.position = startpos;
        }
    }
    void Update()
    {
        
        
        if (isDragging) {
            
            Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
            transform.Translate(mousePosition); 
        }
    }
}

The two chessBoard variables that I put asterisks around cause the error in the title, even though there is no way for it to not exist in the context. I’m stumped on how to fix this so I could use some help.

Whenever someone says “it is this cannot be because X is clearly not the case” - then X is mostly exactly the case:

Any scope begins with curly brackets and ends with curly brackets.
So the if-part and the else-part of the statement are 2 different scopes.

in your case you define the variable chessboard inside the if-scope. But not within the else-scope.

This way it will work:

         //define the local variable outside of the if/else scope.
         ChessBoard chessBoard = new ChessBoard();
         if (Initializedcheck == false)
         {
               precol = (int)Mathf.Round(startpos.x);
               prerow = (int)Mathf.Round(startpos.y);
               col = (int)Mathf.Round(transform.position.x);
               row = (int)Mathf.Round(transform.position.y);
               chessBoard.MovePiece(prerow, precol, row, col);
               Initializedcheck = true;
         }
         else
         {
               precol = (int)Mathf.Round(startpos.x);
               prerow = (int)Mathf.Round(startpos.y);
               col = (int)Mathf.Round(transform.position.x);
               row = (int)Mathf.Round(transform.position.y);
               chessBoard.MovePiece(prerow, precol, row, col);
         }

Though i am not really sure why you have this if-statement either way, since both do exactly the same thing. So from what i can see this should do the same:

         //define the local variable outside of the if/else scope.
         ChessBoard chessBoard = new ChessBoard();
         if (Initializedcheck == false)
         {
               Initializedcheck = true;
         }

         precol = (int)Mathf.Round(startpos.x);
         prerow = (int)Mathf.Round(startpos.y);
         col = (int)Mathf.Round(transform.position.x);
         row = (int)Mathf.Round(transform.position.y);
         chessBoard.MovePiece(prerow, precol, row, col);