what do you want to happen? if you only want to destroy the object clicked, you could try OnMouseDown:
otherwise how do you want to choose which one is destroyed?
EDIT: I had only meant to leave a comment, but since I accidentally put this as an answer, guess I’ll go ahead and elaborate
the reason every object is being destroyed is because every object has the code:
if (Input.GetMouseButtonDown(1)){
Destroy(gameObject);
}
therefore, on every mouse click, every object with this script destroys itself…
if you instead say:
function OnMouseDown (){
ScoreLives.score += 150;
Instantiate(poof, transform.position, transform.rotation);
Destroy(gameObject);
}
it will only affect the object actually clicked (only works with the left button tho)… but if you want to use the right mouse button, you can try:
function OnMouseOver () {
if (Input.GetMouseButton(1)) {
// etc...
}
}
this is all assuming you want to destroy the object which is clicked… But if it was some other factor that would determine the object to destroy, you would likely want a single controlling script checking all your clones and finding the right one to destroy… for example:
(on only one object in the scene)
function Update(){
if (Input.GetMouseButtonDown(1)){ // on right click..
var enemies : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy"); // find all enemies in scene..
for(var i = 0; i < enemies.Length; i++){ // for loop to cycle through list of all enemies
var enemyScript = enemies*.GetComponent(EnemyScript); // example..*
if(enemyScript.health < 2){ // condition to check…
Destroy(enemies*); //destroy the appropriate object(s)*
}
}
}
}
or at least you will want some additional requirement to meet to reach the destroy code… for example:
(on each object in the scene [as before])
function Update(){
var other : Transform = GameObject.Find(“Player”).GetComponent(Transform);
if (other) {
var dist = Vector3.Distance(other.position, transform.position);
// only destroy self if within a certain range of player
if (Input.GetMouseButtonDown(1) && dist < 10){
ScoreLives.score += 150;
Instantiate(poof, transform.position, transform.rotation);
Destroy(gameObject);
}
}
}