need Block Placer/Eraser to not destroy any other gameobjects

As I am a noob to Unity I need some help.
I tried to make a game with block placing/erasing and used this script:

var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
 
function Update () {
    if (Input.GetMouseButtonDown(0))
        Build();
    if (Input.GetMouseButtonDown(1))
        Erase();
}
 
function Build() {
    if (HitBlock()) {
        var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = hit.transform.position + hit.normal;
    }
}
 
function Erase() {
    if (HitBlock())
       
        Destroy(hit.transform.gameObject);
}
 
function HitBlock() : boolean {
    return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}

The problem is that the script is also destroying my terrain and any other gameobject.
I want to somehow detect if the object that is hit by the ray is a cube that is allowed to be destroyed or a object that shouldn’t be destroyed.

There are many ways of achieving this. One of the simplest ways that does not require extra scripts could be to just name the object something specific when you create it. That way you can check if the gameObect the raycast hit has the same name or not and then destroy it if it has.

Example:

function Build() {
    if (HitBlock()) {
        var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = hit.transform.position + hit.normal;
        cube.name = "ErasableCube";
    }
}
 
function Erase() {
    if (HitBlock())
        if(hit.name == "EraseableCube") {
            Destroy(hit.transform.gameObject);
        }
}

You could also do this using the “Tag” property of gameObjects: Tag. You will need to create the tags in the tagmanager first though.