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

help with buy button

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

  1. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    I have a character select screen and a level select screen with 5 characters and 5 levels I have a coin system set up but I can't figure out how to set up a button to buy the level and subtract the coins and have the button stay gone. or would it be better two have two separate buttons one to buy and one to play how would I set up a condition that you cant press play until the buy button is pressed if that is an easier way to do it?
     
  2. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Code (CSharp):
    1. bool lvl5 = false; //Level is locked/unlocked
    2. int lvl5cost = 500;
    3. gameObject lvl5btn; //Put 'gameObject' button for level here
    4.  
    5. void Buy()
    6. {
    7.         If (currentCoins => lvl5cost) //Checks if player has enough coins
    8.         {
    9.                currentCoins -= lvl5cost;
    10.                lvl5 = true; //Level is unlocked
    11.                lvl5btn = SetActive(false) //Button is inactive/gone/non-existant etc
    12.         }
    13. }
    14.  
    15. void Play ()
    16. {
    17.         If (lvl5 = true) //Checks if level is unlocked
    18.         {
    19.                //Play level
    20.         }
    21.         Else
    22.         {
    23.                //Level not bought message
    24.         }
    25. }
    26.  
    The above code might be abit wrong in terms of grammar, but that is the general gist of it.
    Also watch the below how to get the buttons working/putting it all togeather.
     
  3. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    I tried your code but I keep on getting this error CS0246: The type or namespace name 'BuySport' could not be found (are you missing a using directive or an assembly reference?) I tried creating an empty game object and naming it BuySport and I also tried public GameObject BuySport; and I can't seem to figure out how to clear this error
     
  4. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    I got it working but the button doesn't go away or deactivate after I press it?
     
  5. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Well Done.

    Have you put the Button GameObject into the 'lvl5btn' variable in the inspector?
    Oh also make it public, forgot that.
    public gameObject lvl5btn; //Put 'gameObject' button for level here
     
  6. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    yes I got it working completely but how do I make the button stay gone after I reload the game, when I go back in the button comes back
     
  7. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    If your talking about rebooting the game (loading the game up when you come back) then obviously you need a way to save your game. That is something I am currently trying to wrap my head around as there seems to be 3 different ways to save a game and you need to specify which data is to be saved and how to load it back in.

    Sadly I cannot be of any more assistance here as this is the part I am literally trying to figure out now.
    I would suggest looking up 'Unity save binary' or 'Unity save JSON'. One makes a save file that cannot be read by a human as it is encrypted and the other makes a save file that can be read by a human, therefore they can mod the save data (cheat).
     
  8. jbowers7432

    jbowers7432

    Joined:
    Apr 28, 2020
    Posts:
    16
    I
    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.     }
    finally figured it out its a lot to do just to make a button stay gone but this code is for four different buttons