- Home /
Destroy a specific instantiated clone?
Hi!
The player in my game can walk around in the world and with a mouse click place objects in the world. The objects placed are instantiated from a prefab and are called "Brick(Clone)" in the hierarchy. The code for the instantiate below.
var cube : Transform;
function setcube () { // Get aim position and round to nearest grid space var aimx = Mathf.RoundToInt(transform.position.x + 0); var aimy = Mathf.RoundToInt(transform.position.y + 0); var aimz = Mathf.RoundToInt(transform.position.z + 0);
// Place cube
var cube = Instantiate(cube, Vector3 (aimx, aimy, aimz), Quaternion.identity);
}
It seems to work pretty ok. But then I want the player to be able to remove an object with a right click. I've been at this for a few days but can't think of a way to to it. How do I know which specific instance of a clone to destroy? Destroy(gameObject) just destroys the first created (there could be thousands). I've messed around with OnCollisionEnter functions on the cube to detect when a user interacts with them but it won't work. The clones don't seem to have IDs or any way of getting to a specific one the player clicks on.
Answer by almo · May 30, 2011 at 08:03 PM
Found this in another Unity Answer, just altered it a teeny bit. Searching is good. :)
function Update()
{
if (Input.GetMouseButtonDown(1))
{
var hit : RaycastHit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
{
Destroy(hit.collider);
}
}
}
Ahh, never had the brains to look at Raycasts. Been searching like crazy.
It does work in a way. But i've found two issues that i don't know how to get around.
Firstly the objects i'm trying to destroy don't have rigidbody so it looks like all it does is remove the actual collider from the cube, not the cube itself. (Strangely it will remove the plane that makes up the floor without it having a rigidbody).
Secondly i need to make sure the cubes are all the player can destroy - not other stuff. I stuck it in another if - but it won't recognize the cubes by tag?
try a substring name compare, if it starts with Cube, then destroyable, otherwise not.
You can check hit.collider.tag if you want to check tags. $$anonymous$$aybe Destroy(hit.collider.GameObject) would destroy the whole thing? GameObject referrs to the GameObject the collider is attached to.
Thanks guys! Finally got it working with this code:
// Deletes stuff from the world
if (Input.Get$$anonymous$$ouseButtonDown(1))
{
var hit : RaycastHit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
{
if(hit.collider.gameObject.tag == "Cube"){
Destroy(hit.collider.gameObject);
}
}
}
why are you using a ray cast AND get mouse button down? Just attach the "on$$anonymous$$ouseDown" function to the prefab and use Destroy(this).
Your answer
Follow this Question
Related Questions
Pass a copy of a GameObject as variable to another script? 1 Answer
How to delete instantiated GameObject 4 Answers
How can I destroy my Instance without renaming it? 2 Answers
Instantiate and Destroy an object while crossing a line with its clone also 1 Answer
Why could I delete my clones onlY in the same order of instantiation? 2 Answers