- Home /
The question is answered, right answer was accepted
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.
Answer by brandonhotdo · May 27, 2017 at 11:26 PM
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.
Now I get this really long error:
 NullReferenceException: Object reference not set to an instance of an object
 BuyGenerator.Purchase () (at Assets/Scripts/BuyGenerator.cs:23)
 UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154)
 UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637)
 UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773)
 UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52)
 UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
 UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
 UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
 UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
 UnityEngine.EventSystems.EventSystem:Update()
 
 
In the inspector, is populationScript and purchase empty?
Follow this Question
Related Questions
Destroy () vs nulling an object created in script - c# 1 Answer
GameObject.Destroy(gameObject) does not destroy capsules 2 Answers
Delete object only when a certain cursor is enabled 1 Answer
multiple objects with same script, how to destroy one of them ? 0 Answers
Multiple Cars not working 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                