Deleting a prefab with onclick/touch

Hi,

I am trying to make a game where an object falls from the top(out of view) to the bottom of the screen and i am trying to implement that when the player touches the screen the falling object is removed from the game and in turn adds a point to the player’s score.

I have created the gameObject as a prefab and added the following code but nothing happens when i click on the object;

public class OnClickDestroy : MonoBehaviour {

void OnMouseDown()
{
	Destroy (gameObject);
}

}

i added this code into the inspector for the prefab, but i woundered if the code is not working because i am using ‘Destroy (gameObject);’ in the code and the item falling is a prefab?

thank you

A prefab is basically a copy of a GameObject that’s been saved to file.

A game object in your game may be spawned (instantiated) from a prefab, but none of the GameObjects in your game ARE the prefab (it’s just a save file, after all).

OnMouseDown requires a collider, so check to make sure your GameObject has one of those. Also, make sure there aren’t any objects (even transparent ones) between the camera and the object.

Well try this and place it on your Object.

void Update()
{ 
if (Input.GetMousButtonDown(KeyCode.Mouse0))
{ 
destroy (gameObject)
}
}