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")){
if(showMenu == false)
showMenu = true;
else
showMenu = false;
}
if(showMenu == true){
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);
}
}
}
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.
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.