How to destroy a instatiated object?

Hi there, heres a newbie question. I´ve hit the wall on this one and would be thankful if there is anyone here that can help me?
I have a menu wish buttons instatiates gameobject to the scene but I want to be able to destroy that instatiated object with my right mouse button. In my head this should be an easy one but…no . Not for me.

private var showMenu : boolean = false;

``````var A5_1 : Transform;

var icon1 : Texture2D;

function OnGUI(){

if(GUI.Button(Rect(10,10,170,20), "SYMBOLMENY & VYER")){
else
}
GUI.Box(Rect(10, 35, 170, 667), "");

if (GUI.Button(Rect(15, 40, 50, 50 ), icon1)) {
for (var i : int = 0;i < 1; i++) {
Instantiate (A5_1, Vector3(i * 0.0f, 0.03f, 0.0f), Quaternion.identity);

if(Input.GetMouseButtonDown(1))
Destroy(A5_1);

}
}
}
``````

If you got the time, thank you !

I believe this is what you want:

``````var A5_1 : Transform;
var theObject : GameObject;

var icon1 : Texture2D;

function OnGUI(){

if(GUI.Button(Rect(10,10,170,20), "SYMBOLMENY & VYER")) {
else
}
GUI.Box(Rect(10, 35, 170, 667), "");
if (GUI.Button(Rect(15, 40, 50, 50 ), icon1)) {
if(!theObject) {
theObject = Instantiate (A5_1, Vector3(i * 0.0f, 0.03f, 0.0f), Quaternion.identity);
}
}
if(Input.GetMouseButtonDown(1)) Destroy(theObject);
}

}
``````

Remember the importance of consistent structure and indentation, I found at least one of your brackets without an ending bracket and it was hard to read.

This will create a single object and delete it on right mouse click, if you want to create multiple objects you’ll have to instantiate objects then store them into an array like mentioned above. I can give you an example of this as well if you need.

One tip : Use Update() instead of OnGUI() because OnGUI is called twice by frame.

Here is what you want to do:
(I use an array, but you can use only one var instead)

``````var A5_1 : Transform;

var icon1 : Texture2D;
var showMenu : System.Boolean = false;
var tab : Array = new Array();

function OnGUI()
{
if (GUI.Button(Rect(10,10,170,20), "SYMBOLMENY & VYER"))
{
}

{
GUI.Box(Rect(10, 35, 170, 667), "");

if (GUI.Button(Rect(15, 40, 50, 50 ), icon1))
{
for (var i : int = 0; i<5; i++)
{
var newObj= Instantiate(A5_1, Vector3(i * 0.0f, 0.03f, 0.0f), Quaternion.identity);

}
}
}

}

function Update()
{
if(Input.GetMouseButtonDown(1))
{
for (var j : int = 0;j < tab.length; j++)
{
var myObj : GameObject = tab[j];
Destroy(myObj.gameObject);
}

tab.Clear();
}
}
``````

ZeroKcm
http://www.zerokcm.fr/

I think it’s bad practice to use Input.GetButtonDown in OnGUI()

GetButtonDown is meant for the Update() function from what I know. OnGUI is called multiple times per frame so personally I’d rather use some kind of bool for if the button is down from within the Update() even if it does work in OnGUI.

You should never really have a GUI.Button call with an Input.GetButtonDown call inside of it, this makes no sense to me and probably doesn’t work.

But instantiated objects will delete the same as any other object nothing fancy about it.

You should use a GameObject for your instantiated object not a transform though.

My last tip would be you might have (Clone) at the end of your instantiated object name which can throw people off sometimes when dealing with .name stuff. But I don’t think you have that given your example.