Destroying specific clone object while not destroying others on screen

I am making a game and I have run into another command problem, cloned characters spawn and are destroyed with mouse click, however they are all destroyed when I click no matter where the mouse is pointing. I have tried specific location codes I have found but they do not destroy the characters when saved in script.

function Update(){
	if (Input.GetMouseButtonDown(1)){
	ScoreLives.score += 150;
	Instantiate(poof, transform.position, transform.rotation);
	Destroy(gameObject);
	}
}
ScoreLives.score += 150;
Instantiate(poof, transform.position, transform.rotation);
Destroy(gameObject);
}

}

—This code destroys them, but its a mass kill command to all the clone characters with each click.

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 :slight_smile:

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);
}
}
}

Seth Bergman is right. Input.GetMouseButtonDown(1) is true when the right mouse button is pressed. It does NOT check whether the Cursor is actually on your game object.

You could use the OnMouseDown handler but that only triggers on left clicks:

function OnMouseDown()
{
    ScoreLives.score += 150;
    Instantiate(poof, transform.position, transform.rotation);
    Destroy(gameObject);
}

However, as described in the last post of this topic, you can do it like this do detect a right click (OnMouseOver() gets triggered on the GameObject the cursor points on. Then you check for the right mouse button):

function OnMouseOver()
{
    if (Input.GetMouseButtonDown(1))
    {
        ScoreLives.score += 150;
        Instantiate(poof, transform.position, transform.rotation);
        Destroy(gameObject);
    }
}

Judging from your code, it’s placed on the clones, right? If that is the case, every one of the clones is checking the mouse button and destroying itself. It is VERY VERY bad practice to write Input code in different places, since problems like this might arise. What you must do is move all of your input code to one single class. In that class, you must check Input.GetMouseButtonDown as you do now, and if the mouse was indeed clicked this frame, you must find out which clone the mouse is pointing at (see below) and tell that specific clone to destroy itself. Your code should look like this in the end:


void Update()
{
if (Input.GetMouseButtonDown(1)){
Ray r = Camera.main.ScreenPointToRay(input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(r, out hit)){
Clone c = hit.gameObject.GetComponent<Clone>(); //I'm assuming you have a script called Clone which is attached to the clones.
if (c != null)
c.Annihilate(); //and that the Clone script has a function called Annihilate which handles spawning the "poof" and destroying itself.
}
}
}

If you don’t know the functions I used above, look them up in the Unity doc. Also, I wrote this code from memory, so forgive any possible misspellings plz :smiley:

Man, this is a mass suicide script! Every object with this script will suicide when the right button is pressed. @Seth Bergman’s suggestion would be the best alternative if you were reading the left mouse button, but unfortunately the right button doesn’t generate OnMouseDown events - thus you must use raycast to select the victim. A simple way to do that is to find the clicked enemy with the code below (attached to the camera) and call the enemy suicide code with SendMessage:

Camera Script:

function Update(){
  if (Input.GetMouseButtonDown(1)){
    var ray = camera.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;
    if (Physics.Raycast(ray, hit)){
      // call the function DieBastard (if any) in the clicked object:
      hit.transform.SendMessage("DieBastard", null, SendMessageOptions.DontRequireReceiver);
    }
  }
}

Enemy script:

function DieBastard(){
  ScoreLives.score += 150;
  Instantiate(poof, transform.position, transform.rotation);
  Destroy(gameObject);
}

var clicked : boolean = false;
var hit : RaycastHit;

function Update() {
    if(Input.GetMouseButtonDown(0) &&
       collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,
                        Mathf.Infinity)) {
        clicked = !clicked;
        if(tag == "Selected") tag = "Unselected";
        else tag = "Selected";
        if(tag == "Unselected")
        ScoreLives.score += 150;
		Instantiate(poof, transform.position, transform.rotation);
        Destroy(gameObject);
        Destroy(poof);
    }
}
Found a new method to recognize individual clones, using tag switching a boolean 

``and the raycast system for screen location. Thanks for help everyone I would have
been able to recognize the solution without so many different options to learn and draw from.