How to get rid of objects with right click

I have a problem with a very simple script I have. I am making a parody of minecraft that has a different way of placing cubes. I just don’t know how to get rid of the cubes with right click when the spawn point is touching the cube. Please help.
(obviously edited from a shooting script)

var bulletprefab:Transform;


function Update () {
if(Input.GetMouseButtonDown(0))
{
    var Bullet = Instantiate(bulletprefab, GameObject.Find("Spawn point").transform.position, Quaternion.identity);
}

}
@script RequireComponent(CharacterController)

I see that you are creating an object. where are you trying to destroy it?

2 Answers

2

You question is a confusing when you say “when the spawn point is touching the cube.” I’m going to assume because you are using a right mouse click that you want to destroy the the object under the mouse. Assume you named all the objects “Mine.” Here is a bit of code (Untested) to do that job:

if (Input.GetMouseButtonDown(1)) {
	var hit : RaycastHit;
	var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	if (Physics.Raycast(ray, hit)) {
		if (hit.collider.name == "Mine")
			Destroy(hit.collider.gameObject);
	}
}

It uses raycasting. There are a bizillion questions about raycasting on this list that you can search out and read. Reference: Physics.Raycast().

You question is a confusing when you say “when the spawn point is touching the cube.” I’m going to assume because you are using a right mouse click that you want to destroy the the object under the mouse. Assume you named all the objects “Mine.” Here is a bit of code (Untested) to do that job:

if (Input.GetMouseButtonDown(1)) {
    var hit : RaycastHit;
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, hit)) { if (hit.collider.name == "Mine")
        Destroy(hit.collider.gameObject);
    }
}

It uses raycasting. There are a bizillion questions about raycasting on this list that you can search out and read. Reference: Physics.Raycast().

well, sadly it did not work. you see, i use an actual spawnpoint to place cubes with left click. But, I wanted to actually mine the cube by touching the spawn point to the cube and pressing right click. also, what do you mean by where am i going to destroy it?