GameObjects being instantiated on top of each other

I want my character to move and place cubes behind itself as it walks. When I step on a cube that already exists another one is instantiated on top of it. How do I go about changing this?

//#pragma strict
var endCube = gameObject;
var prefab = gameObject;

var collide : boolean = false;

function OnCollisionEnter(coll: Collision){   //Colliding with object?
		if(coll.gameObject.tag=="floor"){
			collide=true;	
			Debug.Log("collision On");
			}
		else{
			collide=false;
				Debug.Log("collision off");
		}
}
function OnCollisionExit(coll: Collision){
			collide=false;			
			Debug.Log("Stay");
}


function Update () {
myPosition = transform.position;

	if(Input.GetButtonDown("up")){
		transform.position.y = transform.position.y+1;
		Instantiate(prefab, transform.position, Quaternion.identity);
	}
	if(Input.GetButtonDown("down")){
		transform.position.y = transform.position.y-1;		
		Instantiate(prefab, transform.position, Quaternion.identity);
	}
	if(Input.GetButtonDown("left")){
		transform.position.x = transform.position.x-1; 
		Instantiate(prefab, transform.position, Quaternion.identity);
	}
	if(Input.GetButtonDown("right")){
		transform.position.x = transform.position.x+1;
		Instantiate(prefab, transform.position, Quaternion.identity);
	}
	if (Input.GetKeyDown (KeyCode.Space)){
		Instantiate(endCube, transform.position, Quaternion.identity);
	}
	
	Debug.Log(myPosition);	
/*	if (collide == false){
		Instantiate(prefab, transform.position, Quaternion.identity);
			Debug.Log("collide = false");*/
	
}

Similar to @richyrich’s idea, you could have a list of Vector3’s that store the positions of the cubes you have already placed:

private List<Vector3> BlockPositions = new List<Vector3>;

Before moving and placing a block you could check to see if the position already exists. If it doesn’t then move and place the block and also add the new position to the list.

if(Input.GetButtonDown("up")){
         transform.position.y+=1;
         If (!BlockPositions.Exists(transform.position))
         {
             //Path is clear, move the player, instantiate a cube and add the position to the list
             Instantiate(prefab, transform.position, Quaternion.identity);
             BlockPositions.Add(transform.position);
         }
         else
         {
             //Path is not clear, move the player back
             transform.position.y-=1;
         }
     }

Not in a position to test the code but you get the general idea.

Note: This method would require that your character/blocks are always in the exact position!

Good luck!

If (!BlockPositions.Exists(transform.position))

  1. Assumes that each block is no more than 1 unit in height - maybe it is

  2. Assumes that the player position starts from and can only exist in whole numbers rather than floats (Vector3 = float, float, float) this seems less likely

    {
    //Path is not clear, move the player back
    transform.position.y-=1;
    }

  3. This now means that once you have laid one cube in a location, the character is frozen