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

Making a button stay gone

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

  1. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    I have made a button to where it is a one-time click to purchase something and when I click it, it disappears, but when I reload the game the button comes back how can I make it stay gone on reload.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    This question boils down to the question: how can I make data persist between scenes, or even between running the game. Saving data (called 'persisting if you want to sound fancy, or even 'serializing') can be simplistic (look at PlayerPrefs), but usually requires some more effort saving your data into files (most cases work well whenyou use JSON, and any of the many good free JSON utilities). Saving data isn't difficult at all, but still something that requires some up-front design.
     
  3. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    I tried to put playerprefs in my onclick function like below but it didn't work I don't know if I have it in the wrong place or if I need to recall it in the start function I tried different things but couldn't get it to work. BuyCity is the name of my button
    Code (CSharp):
    1. public void Buy1()
    2.     {
    3.         if (moneyAmount >= Citycost)
    4.         {
    5.             moneyAmount = moneyAmount - Citycost;
    6.             BuyCity.SetActive(false);
    7.             PlayerPrefs.SetInt("BuyCity", 1);
    8.         }
    9.     }
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It looks well, so you need to be more specific what does not work.

    You may need to add a PlayerPrefs.Save().

    That being said, where do you read the saved value?
     
  5. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    I put a playerprefs.getint("BuyCity", 1) in the start function do I need to change the 1 to something else or move that line somewhere else
     
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Since we can't see your script, it would help if you posted the entire code and explain what you did.
     
  7. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    this is my entire code the buy1 - 4 are my on click functions I just have the buysport playerprefs for now
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class CharacterSelecter : MonoBehaviour
    8. {
    9.  
    10.    
    11.     public Text moneyAmountText;
    12.     public static int moneyAmount;
    13.      //Level is locked/unlocked
    14.     int Sportcost = 50;
    15.     int Cafecost = 20;
    16.     int Cruisercost = 30;
    17.     int Ghostcost = 40;
    18.     public GameObject BuySport;
    19.     public GameObject BuyCafe;
    20.     public GameObject BuyCruiser;
    21.     public GameObject BuyGhost;
    22.  
    23.     public void Buy1()
    24.     {
    25.         if (moneyAmount >= Sportcost)
    26.         {
    27.             moneyAmount = moneyAmount - Sportcost;
    28.             BuySport.SetActive(false);
    29.             PlayerPrefs.SetInt("BuySport", 1);
    30.         }
    31.     }
    32.  
    33.     public void Buy2()
    34.     {
    35.         if (moneyAmount >= Cafecost)
    36.         {
    37.             moneyAmount = moneyAmount - Cafecost;
    38.             BuyCafe.SetActive(false);
    39.         }
    40.     }
    41.  
    42.     public void Buy3()
    43.     {
    44.         if (moneyAmount >= Cruisercost)
    45.         {
    46.             moneyAmount = moneyAmount - Cruisercost;
    47.             BuyCruiser.SetActive(false);
    48.         }
    49.     }
    50.  
    51.     public void Buy4()
    52.     {
    53.         if (moneyAmount >= Ghostcost)
    54.         {
    55.             moneyAmount = moneyAmount - Ghostcost;
    56.             BuyGhost.SetActive(false);
    57.         }
    58.     }
    59.  
    60.     // Start is called before the first frame update
    61.     void Start()
    62.     {
    63.         moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
    64.         PlayerPrefs.GetInt("Buysport", 1);
    65.  
    66.     }
    67.  
    68.     // Update is called once per frame
    69.     void Update()
    70.     {
    71.         moneyAmountText.text = "" + moneyAmount.ToString() + "¢";
    72.     }
    73.  
    74.     public void ToMenu()
    75.     {
    76.         PlayerPrefs.SetInt("MoneyAmount", moneyAmount);
    77.         SceneManager.LoadScene("Menu");
    78.  
    79.     }
    80.  
    81.     public void ToGame()
    82.     {
    83.         PlayerPrefs.SetInt("MoneyAmount", moneyAmount);
    84.         SceneManager.LoadScene("motomadness");
    85.         UnityEngine.Object.FindObjectOfType<CSSwitcher>().SetCharacter(CSSwitcher.currentIndex);
    86.     }
    87. }
    88.  
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're not doing anything with the value you get from GetInt on line 64. What do you expect that to do? You need to get the value, store it somewhere, and make a decision on what to do with the button based on the value. Right now you're just getting the value and throwing it away, so of course it isn't working.
     
  9. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    That’s why I am asking for help I thought line 29 would save the buysport button to setactive false and then line 64 would read that buysport was false and keep it gone. what code would I use?
     
  10. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Ah. This is a slight misconception how programming languages (and C# in particular) work. When you use PlayerPrefs to save the value of a variable under a name (e.g. PlayerPrefs.SetInt("BuySport", 1); ), you are not creating a connection between a variable of that name in you script and the player prefs. All that happens is that there now is an entry somewhere in a table that has a title 'BiySport' and a value of 1.

    In order to restore these values you have to explicitely do that, i.e.

    Code (CSharp):
    1. buysport = PlayerPrefs.GetInt("Buysport", 0);
    Otherwise the value simply gets read, and immediately discarded because C# does not know what you want to do with the value, and it doesn't care that you have a variable that just so happens to be named exactly like the entry in the player prefs.

    Note also that when retrieving the value from player prefs you need to do something sensible with it. You are retireving an integer value; Buysport is of tye GameObject, so tehre probably needs to be some logic that connects the two.
     
  11. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    i tried that but it just says cant convert int to gameobject
     
  12. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    i finally figured it out this is what i did
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class CharacterSelecter : MonoBehaviour
    8. {
    9.  
    10.    
    11.     public Text moneyAmountText;
    12.     public static int moneyAmount;
    13.      //Level is locked/unlocked
    14.     int Sportcost = 50;
    15.     int Cafecost = 20;
    16.     int Cruisercost = 30;
    17.     int Ghostcost = 40;
    18.     public GameObject BuySport;
    19.     public GameObject BuyCafe;
    20.     public GameObject BuyCruiser;
    21.     public GameObject BuyGhost;
    22.     private bool BuySportActive;
    23.     private bool BuyCafeActive;
    24.     private bool BuyCruiserActive;
    25.     private bool BuyGhostActive;
    26.  
    27.  
    28.         private void TryActivateTheBuySport()
    29.     {
    30.         if (!BuySportActive)
    31.         {
    32.             BuySport.SetActive(false);
    33.         }
    34.     }
    35.  
    36.     private void TryActivateTheBuyCafe()
    37.     {
    38.         if (!BuyCafeActive)
    39.         {
    40.             BuyCafe.SetActive(false);
    41.         }
    42.     }
    43.  
    44.     private void TryActivateTheBuyCruiser()
    45.     {
    46.         if (!BuyCruiserActive)
    47.         {
    48.             BuyCruiser.SetActive(false);
    49.         }
    50.     }
    51.  
    52.     private void TryActivateTheBuyGhost()
    53.     {
    54.         if (!BuyGhostActive)
    55.         {
    56.             BuyGhost.SetActive(false);
    57.         }
    58.     }
    59.  
    60.     private void VerifyIfButtonAIsEnabledInTheSaveData()
    61.     {
    62.         BuySportActive = (PlayerPrefs.GetInt("BuySport", 1)) == 1 ? true : false;
    63.         BuyCafeActive = (PlayerPrefs.GetInt("BuyCafe", 1)) == 1 ? true : false;
    64.         BuyCruiserActive = (PlayerPrefs.GetInt("BuyCruiser", 1)) == 1 ? true : false;
    65.         BuyGhostActive = (PlayerPrefs.GetInt("BuyGhost", 1)) == 1 ? true : false;
    66.     }
    67.  
    68.     private void SaveTheSportButtonValue()
    69.     {
    70.         PlayerPrefs.SetInt("BuySport", 0);
    71.     }
    72.  
    73.     private void SaveTheCafeButtonValue()
    74.     {
    75.         PlayerPrefs.SetInt("BuyCafe", 0);
    76.     }
    77.  
    78.     private void SaveTheCruiserButtonValue()
    79.     {
    80.         PlayerPrefs.SetInt("BuyCruiser", 0);
    81.     }
    82.  
    83.     private void SaveTheGhostButtonValue()
    84.     {
    85.         PlayerPrefs.SetInt("BuyGhost", 0);
    86.     }
    87.  
    88.     public void Buy1()
    89.     {
    90.         if (moneyAmount >= Sportcost)
    91.         {
    92.             moneyAmount = moneyAmount - Sportcost;
    93.             BuySport.SetActive(false);
    94.  
    95.             if (BuySportActive)
    96.             {
    97.                 SaveTheSportButtonValue();
    98.             }
    99.  
    100.         }
    101.     }
    102.  
    103.     public void Buy2()
    104.     {
    105.         if (moneyAmount >= Cafecost)
    106.         {
    107.             moneyAmount = moneyAmount - Cafecost;
    108.             BuyCafe.SetActive(false);
    109.  
    110.             if (BuyCafeActive)
    111.             {
    112.                 SaveTheCafeButtonValue();
    113.             }
    114.         }
    115.     }
    116.  
    117.     public void Buy3()
    118.     {
    119.         if (moneyAmount >= Cruisercost)
    120.         {
    121.             moneyAmount = moneyAmount - Cruisercost;
    122.             BuyCruiser.SetActive(false);
    123.             if (BuyCruiserActive)
    124.             {
    125.                 SaveTheCruiserButtonValue();
    126.             }
    127.         }
    128.     }
    129.  
    130.     public void Buy4()
    131.     {
    132.         if (moneyAmount >= Ghostcost)
    133.         {
    134.             moneyAmount = moneyAmount - Ghostcost;
    135.             BuyGhost.SetActive(false);
    136.             if (BuyGhostActive)
    137.             {
    138.                 SaveTheGhostButtonValue();
    139.             }
    140.         }
    141.     }
    142.  
    143.     // Start is called before the first frame update
    144.     private void Awake()
    145.     {
    146.         VerifyIfButtonAIsEnabledInTheSaveData();
    147.         TryActivateTheBuySport();
    148.         TryActivateTheBuyCafe();
    149.         TryActivateTheBuyCruiser();
    150.         TryActivateTheBuyGhost();
    151.     }
    152.  
    153.  
    154.  
    155.     void Start()
    156.     {
    157.         moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
    158.        
    159.  
    160.     }
    161.  
    162.     // Update is called once per frame
    163.     void Update()
    164.     {
    165.         moneyAmountText.text = "" + moneyAmount.ToString() + "¢";
    166.         TryActivateTheBuySport();
    167.         TryActivateTheBuyCafe();
    168.         TryActivateTheBuyCruiser();
    169.         TryActivateTheBuyGhost();
    170.     }