Destroy(Object) does not work on this script, why?

Hello,
I am trying to destroy an object but cannot make it work, could you point me out the reason for it? Here is the script that i wrote below;

var AnyObject : GameObject;

function Update ()
{
    if(Input.GetMouseButton(0))
    {
    var Clone : GameObject;
    Clone = Instantiate(AnyObject, AnyObject.transform.position, Quaternion.identity);
    Clone.name = "Clone";
    }
    if(Input.GetMouseButton(1))
    {
    Destroy(Clone);
    }
}

Thank you in advance!

Outside the “if” is no better. That variable only exists until you exit the Update() method. The next time you come into the method, you declare (create) yet another new “Clone” variable that doesn’t have anything in it (unless you were pressing button 0 also).

You need to store your Clone in a variable that exists outside the scope of Update().
If you need to store many of them, you need an ArrayList or an Array

#pragma strict

 var AnyObject : GameObject;
 var Clones : Array = new Array();

 function Update ()
 {
     if(Input.GetMouseButton(0))
     {
	     var Clone : GameObject;
	     Clone = Instantiate(AnyObject, AnyObject.transform.position, Quaternion.identity);
	     Clone.name = "Clone";
	     
	     // store the clone to an array outside the method
	     // otherwise the variable won't exist outside the method
	     Clones.Add(Clone); 
     }
     if(Input.GetMouseButton(1))
     {
     	// destroy item at index 0
     	Destroy(Clones[0]);
     	// remove the emptied index from the array 
     	Clones.RemoveAt(0);
     }
 }

I wrote the code for you where you can instantiate multiple objects and destroy them one after the other. but before know what was wrong in your code:

First use Input.GetMouseButtonDown instead of Input.GetMouseButton
As Input.GetMouseButton will return true as long as the button is clicked and will spam your condition and instantiate endless objects.

Same goes for the second one (Input.GetMouseButtonDown(1)).

In order to Instantiate one and destroy it, it’s very easy, simply create a variable outside of your update, fill it in the update, and destroy it on button click.

But if you want to create mutliple objects, you need to keep track of them, so use an array.

As for the code I wrote:
Add it on your camera or any gameobject,
Reference the prefab you want to instantiate.
And test it:

//Object to Instantiate
var AnyObject : GameObject;
//Array to hold the clones
var Clones = new Array();

 function Update ()
 {
     if(Input.GetMouseButtonDown(0))
     {
     	 var Clone : GameObject;
     	 //Instantiate the Object
	     Clone = Instantiate(AnyObject, this.transform.position, Quaternion.identity);
	     //Rename the Object
	     Clone.name = "Clone";
	     //Push it to the Array
		 Clones.push(Clone);
     }
     if(Input.GetMouseButtonDown(1))
     {
     	if(Clones.Count > 0){ //Make sure the Array is not empty
     		Destroy(Clones[Clones.Count - 1]);//Remove the Clone
     		Clones.pop();//Pop it from the array
     	}
     }
 }