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 + "

Cost:" + cost + "
Viewers 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! :slight_smile:

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!

3 Answers

3

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.

Take a button reference in a game object and the deactivate the button using SetActive() method

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!