How to Remove a Button after it has been pressed and executed it's script?
I'd like to have it so that when a button is pressed, AND has successfully completed it's associated script, the button is destroyed and removed from the UI.
Here's my code:
public class UpgradeManager : MonoBehaviour {
public Click click;
public UnityEngine.UI.Text itemInfo;
public float cost;
public int count = 0;
public int clickPower;
public string itemName;
private float baseCost;
void Start(){
baseCost = cost;
}
void Update(){
itemInfo.text = itemName + "\nCost:" + cost + "\nViewers Per Stream: +" + clickPower;
}
public void PurchasedUpgrade(){
if (click.views >= cost) {
click.views -= cost;
count++;
click.viewsperclick += clickPower;
cost = (baseCost * Mathf.Pow (1.15f, count));
}
Any help will be greatly appreciated! :)
As a unity grandmaster, i can advise u to use namespace: using UnityEngine.EventSystems; and then: EventSystem.current.currentSelectedGameObject.SetActive(false);
it will remove the button which was pressed. ur welcome!
Answer by RafiXWPT · Aug 29, 2015 at 10:45 AM
Get refference to that button, then inside method which is runned after click, simply disactivate it:
public GameObject myButton; // Get refference to button
And at the end of button method:
myButton.SetActive(false);
Should work.
Answer by tauqeerahmed · Aug 30, 2015 at 02:44 PM
Take a button reference in a game object and the deactivate the button using SetActive() method
Answer by Fekalis · Jan 22, 2021 at 06:35 PM
As a unity grandmaster, i can advise u to use namespace:
using UnityEngine.EventSystems;
and then:
EventSystem.current.currentSelectedGameObject.SetActive(false);
it will remove the button which was pressed. ur welcome!
Your answer
Follow this Question
Related Questions
canvas child of another canvas issue 0 Answers
GameObject.Destroy(gameObject); Not Working? 0 Answers
how make object follow mouse cursor in 3d space 0 Answers
How to destroy only the collided instance of prefab and not the original one? 0 Answers
Cannot convert type `UnityEngine.GameObject[]' to `UnityEngine.GameObject' via a built-in conversion 1 Answer