Search Unity

Shop System

Discussion in '2D' started by SEVEN-ARTS, Feb 9, 2021.

  1. SEVEN-ARTS

    SEVEN-ARTS

    Joined:
    Aug 31, 2020
    Posts:
    40
    Hello,
    I'm currently working on a 2D game and would like to do a shop system. I would like to withdraw 15 coins when I press a button, but I have no idea how to do it.
    This is the script from the coin system
    (I don't know if you need this to help me):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Points : MonoBehaviour
    7. {
    8.     public Text scoreText;
    9.     public float scoreAmount;
    10.     public float pointIncreasedPerSecond;
    11.  
    12.     void Start()
    13.     {
    14.         scoreAmount = 0f;
    15.         pointIncreasedPerSecond = 0.5f;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         scoreText.text = "" + (int)scoreAmount;
    21.         scoreAmount += pointIncreasedPerSecond * Time.deltaTime;
    22.  
    23.         if (scoreAmount > PlayerPrefs.GetInt("Highscore"))
    24.         {
    25.             PlayerPrefs.SetInt("Highscore", (int)scoreAmount);
    26.         }
    27.     }
    28. }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Have you worked with UI buttons before? Create a public function called ShopWithdraw or something and have it check if scoreAmount is >= 15. If it is, then have scoreAmount -= 15. Click on the UI button you make and go to the events and add an OnClick event, drag in the script with the ShopWithdraw function and select the function. Then when you click it, it will perform that function.

    Now, im guessing the player has an inventory so you want that 15 to go to the Player. Its pretty similar but you add instead of reverse. Ill leave that part up to you but if you get close and post your code, i may help again.
     
    SEVEN-ARTS likes this.
  3. SEVEN-ARTS

    SEVEN-ARTS

    Joined:
    Aug 31, 2020
    Posts:
    40
    Okay, I've now put it in a new script and tried the normal one, but no function is displayed for the button.
    Did I make a mistake in the script and which one should I use, the new one or the old one with the one added?

    Here is the old script with the new code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Points : MonoBehaviour
    7. {
    8.     public Text scoreText;
    9.     public float scoreAmount;
    10.     public float pointIncreasedPerSecond;
    11.     public float ShopWithDraw;
    12.  
    13.     void Start()
    14.     {
    15.         scoreAmount = 0f;
    16.         pointIncreasedPerSecond = 0.5f;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         if (scoreAmount >= 15)
    22.         {
    23.             scoreAmount -= 15;
    24.         }
    25.         scoreText.text = "" + (int)scoreAmount;
    26.         scoreAmount += pointIncreasedPerSecond * Time.deltaTime;
    27.  
    28.         if (scoreAmount > PlayerPrefs.GetInt("Highscore"))
    29.         {
    30.             PlayerPrefs.SetInt("Highscore", (int)scoreAmount);
    31.         }
    32.     }
    33. }
    And here the new one:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GivePoints : MonoBehaviour
    6. {
    7.     public float scoreAmount;
    8.     public float ShopWithDraw;
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.  
    15.     void Update()
    16.     {
    17.        if(scoreAmount >= 15)
    18.         {
    19.             scoreAmount -= 15;
    20.         }
    21.     }
    22. }
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    So you need to make a brand new function, not just an If condition within Update or Start.

    Something like this:
    Code (CSharp):
    1. public void WithdrawCurrency()
    2. {
    3.     if(scoreAmount >= withdrawAmount)
    4.     {
    5.         scoreAmount -= withdrawAmount;
    6.     }
    7. }
    Make sure you declare that function outside of Start, or Update or any other function. It should be outside of the { } of Start or Update but within the { } of the Class. Basically, Start and Update are functions so you need to create your own.

    Once you save and exit, then you should be able to see that function in the event trigger on the UI button.
     
    SEVEN-ARTS likes this.
  5. SEVEN-ARTS

    SEVEN-ARTS

    Joined:
    Aug 31, 2020
    Posts:
    40
    Yeah, thats works!
    Thanks! :)
     
    Cornysam likes this.
  6. SEVEN-ARTS

    SEVEN-ARTS

    Joined:
    Aug 31, 2020
    Posts:
    40

    Hello again, so now I have the script running, I wanted to ask if you can help me with one more thing, because I would like, if you buy something, you bring a prefab into the scene in the script. Do you think, you can help me?
     
  7. SEVEN-ARTS

    SEVEN-ARTS

    Joined:
    Aug 31, 2020
    Posts:
    40
    I have already tested whether it would work in the previous script and yes it does!
     
  8. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Just use the Instantiate method to bring Prefabs into the scene.
     
  9. SEVEN-ARTS

    SEVEN-ARTS

    Joined:
    Aug 31, 2020
    Posts:
    40
    Do you know how to use this?
     
  10. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Yes. but you need to get better at Googling things. Just look up "How to instantiate gameobjects in unity" and you will find plenty.