Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Sp33dy_Sn1pa · Aug 31, 2015 at 12:04 AM · c#playerprefsbuttonsintcoins

Coin Spending/PlayerPref Subtracting

Hello and thanks for taking a look at my issue. I currently have a self-designed Gold Collection System in which the CoinController.cs script holds the value of my coinCount integer. Then, by using CoinBehavior.cs attached to each coin, I am able to add 1 coin to my total amount with CoinController.coinCount++. That all works exactly how I want however, the problem occurs when I attempt to subtract a specific amount. For example, if my Player has 5,000 coins saved and he goes to the store to purchase a 1,000 coin item, how could I make sure that the Player has at least 1,000 coins available so that the amount can never go below 0 coins, then subtract 1,000 coins from the 5,000 coin total, and then save the new coinCount total as 4,000?

I don't need someone to write my whole code, I'm just looking for ideas, tips, and/or strategies that might help me out a little when it comes to spending the coins that have been collected by using my UI button OnClick function to call SpendCoins.cs and then "public void BuyNewCharacter". Thank you in advance for your time and the associated codes are attached below.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using GooglePlayGames;
 using UnityEngine.SocialPlatforms;
 
 public class CoinController : MonoBehaviour
 {
     public static int coinCount = 0;
     
     Text text;
     
     void Awake()
     {
         text = GetComponent<Text>();
         coinCount = PlayerPrefs.GetInt("Gold Count");
     }
     
     void Update()
     {
         text.text = "" + coinCount;
         PlayerPrefs.SetInt("Gold Count", coinCount);
     }
 }

 using UnityEngine;
 using System.Collections;
 
 public class CoinBehavior : MonoBehaviour
 {
     void OnTriggerEnter2D(Collider2D collider)
     {
         switch(collider.gameObject.name)
         {
         case "Player":
             
             CoinController.coinCount++;
             
             Destroy(this.gameObject);
             
             break;
         }
     }
 }

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using GooglePlayGames;
 using UnityEngine.SocialPlatforms;
 
 public class SpendCoins : MonoBehaviour
 {
         public static int coinCount;
     public static int NewCharacterCost = 1000;
     public static int GoldLeft;
 public static int coinsLeft;
         
         void Awake()
     {
         coinsLeft = PlayerPrefs.GetInt("Gold Count");
     }
     
     public void BuyNewCharacter()
     {
         if(coinsLeft > 1000)
         {
         GoldLeft = coinsLeft - NewCharacterCost;
         CoinController.coinCount = GoldLeft;
         PlayerPrefs.SetInt("Gold Count", coinCount);
         }
         }
 }


Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image binaryspace1010 · Aug 31, 2015 at 02:05 AM 1
Share

Well, I can give you some tips, but i have read your code, and don't see a problem! $$anonymous$$ainly, what your doing is sound, so what exactly is the problem, or what is not working right?

avatar image Sp33dy_Sn1pa · Aug 31, 2015 at 03:55 AM 0
Share

I deleted my last reply because I just figured out how to let you know about the problem I am running into. When that button is pressed, its supposed to reassign the value for how many coins are left after the 1,000 has been subtracted. But, if I use the same lines of code from the "public void BuyNewCharacter" button on another button, lets say if I had a "BuyGirlCharacter" button for example, and I change it to

 public static int GirlCharacterCost = 2000;
      
 public void BuyGirlCharacter()
      {
          if(coinsLeft > 2000)
          {
          GoldLeft = coinsLeft - GirlCharacterCost;
          CoinController.coinCount = GoldLeft;
          PlayerPrefs.SetInt("Gold Count", coinCount);
          }


then it subtracts from the original amount and doesn't take into consideration the fact that I had already subtracted 1,000. Something isn't right and I know I'm just overlooking some tiny detail.

avatar image Sp33dy_Sn1pa · Aug 31, 2015 at 04:05 AM 0
Share

Each UI button in theory should:

  1. Check if there is enough gold in "Gold Count" for the purchase.

  2. Subtract the character cost from the amount in "Gold Count".

  3. Re-save the new amount left over in "Gold Count".

  4. Update the CoinController so that coin text is displayed correctly.

  5. Disable the button to prevent multiple purchase.

Hopefully that makes sense. I think that using the same public static integer in multiple places might be causing it but I just can't wrap my head around the logic for this. I've overcome some ridiculous obstacles, but this one is just irritating. Thank again.

avatar image binaryspace1010 · Aug 31, 2015 at 12:19 PM 1
Share

@Sp33dy_Sn1pa Hey, I've thought about what you said, and I have a solution. If you want a button to remove cash, you should make sure that there is only one script and one place that removes it.

example: int currentCash = PlayerPrefs.GetInt ( current cash); int cost = 1,000;

if(currentCash >= 1000) { currentCash = currentCash - cost;

 PlayerPrefs.SetInt (currentCash);

}

And Here's How I Think You Should Disable The Button: (This is)

Button button; Text buttonText;

void Start () { button = GameObject.Find("THE NA$$anonymous$$E OF YOUR BUTTON").GetComponent(); buttonText = GameObject.Find("NA$$anonymous$$E OF BUTTON/NA$$anonymous$$E OF TEXT").GetComponent(); }

public void DisableButton() { button.interactable = false; buttonText.text = "Already Bought";

}

avatar image Sp33dy_Sn1pa · Aug 31, 2015 at 11:16 PM 0
Share

Thank you so much for your assistance! I thought about what you said in regard to having it on the same script and so what I was able to come up with was

 public static int coinCount = 0;
 
 public static int character1Cost = 1000;
 public static int character2Cost = 3000;
 public static int character3Cost = 5000;
 
 public static int goldRemaining;
 
 Text text;
 
 void Awake()
 {
      text = GetComponent<Text>();
      coinCount = PlayerPrefs.GetInt("Gold Count");
 }
 
 public void BuyCharacterOne()
 {
      if(coinCount >= 1000)
      {
           goldRemaining = coinCount - character1Cost;
           coinCount = goldRemaining;
      }
 }
 
 public void BuyCharacterTwo()
 {
      if(coinCount >= 3000)
      {
           goldRemaining = coinCount - character2Cost;
           coinCount = goldRemaining;
      }
 }
 
 public void BuyCharacterThree()
 {
      if(coinCount >= 5000)
      {
           goldRemaining = coinCount - character3Cost;
           coinCount = goldRemaining;
      }
 }
 
 void Update()
 {
      text.text = "" + coinCount;
      PlayerPrefs.SetInt("Gold Count", coinCount);
 }
 }
Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with coin script and operands 1 Answer

Can PlayerPrefs be accessed in different scripts - c# 2 Answers

How to automatically generate a new int with a specific name? 1 Answer

Playerpref Saving Lag 1 Answer

save a int with playerprefs for one instance of a gameobject 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges