I’ve been trying to get it so when I tap a single (moving object) it will destroy that object. But Everytime I tap the screen it destroys all of the objects of this type. Currently I have about 100 spheres falling from the top of the screen to the bottom.

 var Sphere : GameObject;

var counter : Counter;

function Start () {
counter = Camera.main.GetComponent(Counter);
Sphere = GameObject.FindWithTag(“Enemy”);
}

function Update () {
for (var i = 0; i < Input.touchCount; ++i) {
if(Input.GetTouch(i).phase == TouchPhase.Began)
{

   var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
   	     
   //if(Physics.Raycast(ray)){
    if(Sphere.gameObject.transform.position == Physics.Raycast(ray))
    {
    	removeMe(Sphere);
   		counter.addToScore();
    }		
}

}
}

function removeMe (object : GameObject) {
Destroy(object);
}

Try this

function Update () {

//Use the mouse button to select Game Objects
if(Input.GetMouseButtonDown(0)){
	
	var hit : RaycastHit;
	var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);	//Makes a ray sent from the point that was clicked

	//Check if the mouse clicked an object
	if(Physics.Raycast(ray, hit, rayDistance)){
	
		var enemyScript = hit.transform.GetComponent(scriptEnemy);
		enemyScript.numberOfClicks -= 1;
	}
}

}

You would then have your sphere prefab with this script

var numberOfClicks : int = 1;

function Update(){
    if(numberOfClicks <= 0){
         Destroy(gameObject);
    }

}

Try that. Also, are these spheres instances of a prefab that are being instantiated, because if not, that is what you will need to do for this solution.

Note: this script works for clicking the left mouse button, I don’t know if you wanted another button to be used.

Thanks! Everyone I remoted into my mac and fixed it! You made me realize that I was tracking where the user touched on the Enemy script instead of the player controller! of course they all deleted on touch

@Spartan301 Do you know a way to make this work with multiple touch? I have to play with the speed of the objects on the screen as well with this method it seems to only work if I tap right in front of where the object is heading.