Destroying a Button when you click it.

Hello, I am making an idle game. So what I want to do is when I hit 100 people (the currency of the game), I want to be able to click this button where it can unlock a feature. So it will remove 100 from the current amount of currency I have, the button would disappear, and then the feature would appear.

This is my code for what I have but it doesn’t work. The button’s functions just stop working instead of the button itself disappearing:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BuyGenerator : MonoBehaviour {

	public Population populationScript;
	public ProgressBar progressScript;
	public Button purchase;
	public Text info;
	public float cost = 100f;

	private float _newCost; 

	void Update() 
		info.text = " Purchase for: " + cost + " humans.";

	public void Purchase()
		if (populationScript.population >= 100) 
			populationScript.population = populationScript.population - 100;
			purchase.enabled = false;

Anyone have a solution? Thanks.

You are disabling the gameObject’s component, which is the button. Replace purchase.enabled = false with purchase.gameObject.SetActive(false); Of course, you would also need a function to re-enable the button later on ( purchase.gameObject.SetActive(true); ) but I presume you have that in another script.