Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

SetActive not working?

Discussion in 'Scripting' started by jbowers7432, Apr 29, 2020.

  1. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    I have a buy button that works with decreasing my money but when I click it the button doesn't go away I have had the setactive command in the if statement like below and in its own separate onclick function and in the void buy but outside of the if statement any ideas on what's wrong
    Code (CSharp):
    1. public static int moneyAmount;
    2.     bool lvl5 = false; //Level is locked/unlocked
    3.     int lvl5cost = 10;
    4.     public GameObject lvl5btn;
    5.  
    6.     public void Buy()
    7.     {
    8.         if (moneyAmount >= lvl5cost)
    9.         {
    10.             moneyAmount = moneyAmount - lvl5cost;
    11.             lvl5 = true;
    12.             lvl5btn.SetActive(false);
    13.         }
    14.     }
    15.  
    16.     public void ButtonClicked()
    17.     {
    18.         lvl5btn.SetActive(false);
    19.     }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Are the functions connected to the button in the inspector?
    Is the lvl5btn correctly assigned?
    Is it possible some other script is immediately reactivating the buttons?
     
  3. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    ok I figured it out I didn't have the gameobject selected in the inspector script