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.
//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.
Oops. The rayDistance is a float variable that should be among the inspector variables. Basically the spheres must be within that distance from the camera for the raycast to work.
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.
Sorry no. I have not worked with touch screen code before. All I know currently is with keyboard and mouse input.
Oops. The rayDistance is a float variable that should be among the inspector variables. Basically the spheres must be within that distance from the camera for the raycast to work.
– Spartan301