Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by upariver · Nov 10, 2016 at 03:20 PM · destroysetactiveenabled

Remove button in runtime

I have a instance of a button that I want removed. For that , when I click on one of the buttons, it sets himself as the Button current_button variable. I want to remove the current_button, but I have tried different things with no luck so far.

SetActive with current_button.getComponent().SetActive(false);

current_button.enabled = false;

Destroy(current_button);

And a few other methods. Could anyone point me how I could achieve this? Thanks for the help.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Desoxi · Nov 10, 2016 at 03:36 PM 1
Share

If you just have two buttons which you want to remove when you click on them. Just create a script with the following public method:

public void Destroy$$anonymous$$e() { Destroy(gameObject); }

Attach this script to you button and put it into the OnClick event on the button. Now you should be able to destroy buttons you are clicking on.

avatar image upariver Desoxi · Nov 10, 2016 at 05:40 PM 0
Share

Thank you for the help!

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Suddoha · Nov 10, 2016 at 03:47 PM

Your approaches only destroy the button component. A button usually has a graphics target (mostly an image). If you only destroy the button component, the graphics target will remain on the object.

Either destroy both, or just the complete button's GameObject:

 Destroy(current_button.gameObject);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image upariver · Nov 10, 2016 at 05:40 PM 0
Share

Thank you very much!

avatar image
0

Answer by DarthHawk13 · Apr 29, 2020 at 02:05 AM

From my experience the best way to delete a UI button at runtime, or any UI element, is to do the following with the UI element; make it a child of a GameObject and do away with the GameObject, or hide it.

    /*Lets say we generated UI buttons at runtime and named them "1","2, and so forth.
  You can do this easily when you instantiate the UI element/button with the following code
 
 
 (variable name for the UI element).name=i.ToString();
 
 Let's say our code kept track of an int we named 'i' and every time i was used to create a variable
  name the next line of code incremented i by 1. We made each button created at runtime a child of an empty GameObject whose sole purpose is to nicely store our buttons in the Editor Hierarchy. The variable name in our code referring to this GameObject is "buttonHolder"
 
   Now when a certain condition is met we want to remove a specific UI button.
    Let's say the UI button we named "3".
    We need to locate it first in the code in order to either hide it or remove it.*/
   
   
   void DestroyUI_Stuff(string buttonName){ 
   
   //create a GameObject at runtime in code and give it a name. 
   //If we see it in the editor during testing/play mode something went wrong...
   GameObject noMore = new GameObject("IShouldNotExist!");
   
   foreach (Transform child in buttonHolder) //even Button UI elements have a transform 
               {
                   if (child.name == buttonName)
                   {
                    child.transform.SetParent(noMore.GetComponent<Transform>());
                       Destroy(noMore); //destroy the button (evil laugh) 
   //noMore.SetActive(false);  <--this code will turn it off instead of destroying it.
                   }
               }
  
  }
 
 

 
 

Running this method/function looks like this:

 DestroyUI_Stuff("3");
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by BielJunio · Apr 29, 2021 at 11:14 PM

@upariver To you get a UI component, import to your C# script the UnityEngine.UI.

 using UnityEngine.UI;

Then you can get the Component reference to this in your code.

...

 gameObject.GetComponent<Button>();

...

And Destroy this

 Destroy(gameObject.GetComponent<Button>());

But if you trying remove the object with a Button Component, you just need Destroy the GameObject.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate() and Destroy() vs setActive() 1 Answer

Canvas not de-activated when played in APK (works in unity editor) 0 Answers

gameObject reappearing after setactive false 1 Answer

OnCollisionEnter - Enabled C# 1 Answer

SetActive is destroying objects instead of settings them as false 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges