This script destroys all of the gameobjects that it is attached to and I have no idea why.
function Update()
{
if(Input.GetMouseButtonDown(0))
{
Destroy(gameObject);
}
}
For some reason when I click on one of the prefabs all of them are destroyed at the same time. I have no idea how to fix this. I’ve tried using this.gameObject as well and this has no effect on how the code works.
Because you never check to see that the cursor is actually over any particular object. That script says: “Did he press the mouse button? Yep? Okay, then, destroy myself!”. You need to either raycast, and compare (gameObject == raycastHit.gameObject); or, you can use the function OnMouseDown, which essentially does the same thing, in the background.
The function OnMouseDown doesn’t seem to be working. I’ve put this.
function OnMouseDown()
{
Destroy(gameObject);
}
If this is right could you please explain how to do the raycasting? If not though, could you point out my mistake?
Does your object have a collider on it?
Well… that was simpler than I thought it would be. Thanks so much for that though. I can’t believe I forgot something so simple!