- Home /
how can I delete a UI button?
Hello, I have a canvas set up with few images and one button. The button is used to log in. I would like to delete the button from the canvas after the user logs in, but I do not want to destroy the actual canvas, or images in the canvas. I only want to destroy the button. How can I do this?
It is not good approach to delete and instantiate gameobjects during the game or app running. You should use enable and disable feature. :)
Answer by NeverHopeless · Jul 10, 2015 at 04:45 AM
Perhaps using this on a certain event:
Destroy(yourButtonGameObject);
tried this... it destroys the entire gameobject holding the canvas... so everything gets destroyed
I just tried it with this hierarchy:
$$anonymous$$ainCamera
GameObjectA (this destroys gameObject B)
Canvas
-----Panel
---------GameObjectB
I end up with this:
$$anonymous$$ainCamera
GameObjectA (this destroys gameObject B)
Canvas
-----Panel
$$anonymous$$ay I see your hierarchy ?
Answer by TruffelsAndOranges · Jul 10, 2015 at 05:10 AM
You can also disable it:
yourButtonGameObject.enabled = false;
Destroying your UI seems kinda weird, and I'd recommend doing this before Destroy( ... ) for most cases.