• 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 /
avatar image
0
Question by truedreams · Sep 13, 2017 at 07:13 AM · unity 5.2

shop menu coins not incrementing

hello sir , i am new to unity and i have learned C# language actually i am making a game like flappy bird in which i have a shop menu i want to save coins in shop menu and i know for that i have to use playerprefsget int and setint . i am able to save the coins but after i play the level it save that coin in shop menu .

i mean to say its not incrementing like if i have 15 saved coins and if i play back and collect 16 coins i get 16 coins only in my shop menu instead of 31..thats the makor problem i am facing .

please help me with my this problem . although my code is:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class purchase : MonoBehaviour {

 // private int currentvalue;
  private int gettingvalue;
  [SerializeField]
  private Text cointext;
 int totalvalue;
 int currentvalue ;
   

 private void Awake()
  {
     addcoins();
    

 }
  private void Update()
  {

 //   



 }
 public void addcoins()
 {
   
     
         gettingvalue = treetry.instance.finalscore;
         currentvalue += currentvalue + gettingvalue;
         totalvalue += currentvalue + totalvalue;
         cointext.text = totalvalue.ToString();
         PlayerPrefs.SetInt("totalcoins", totalvalue);
         PlayerPrefs.GetInt("totalcoins", totalvalue);
         PlayerPrefs.Save();
     
 
 }

}

this code i have wriiten myself .

please help me your help is appreciated.. thank you in advance.

Comment
Add comment · Show 5
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 truedreams · Sep 14, 2017 at 12:38 PM 0
Share

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class treetry : $$anonymous$$onoBehaviour {

 public static treetry  instance;
 [SerializeField]
 private GameObject endingpanel;

 [SerializeField]
 private Text coinscollect;

 [HideInInspector]
 public int finalscore;

// private float countallcoins = 0;

 private void Awake()
 {
     makeinstance();
 }

 // Use this for initialization
 void makeinstance()
 {

     if (instance == null)
     {
         instance = this;
     }

 }

 // Update is called once per frame
 void Update()
 {
      coinscollect.text = finalscore.ToString(); // b1.coincount.ToString(); //+ countallcoins.ToString();
      PlayerPrefs.SetInt("coin", finalscore);
      finalscore = birdmovement.instance.coincount;

 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.tag == "Player")
     {
         Destroy(collision.gameObject);
          endingpanel.SetActive(true);
        finalscore = birdmovement.instance.coincount;

        
        // mcpanel.SetActive(true);
     }
 }

}

Show more comments
avatar image truedreams · Sep 18, 2017 at 01:45 PM 0
Share

this coding is able to increment coins but its incrementing whenever i go to shop menu for example 15 + 15 = 30 and if i go back to shop menu this will be 30 + 15 because of contnious incrementation .

      gettingvalue = PlayerPrefs.GetInt("totalcoins", currentvalue) + treetry.instance.finalscore;
      PlayerPrefs.Save();
      currentvalue = currentvalue + gettingvalue;
      cointext.text = currentvalue.ToString();
      PlayerPrefs.SetInt("totalcoins", currentvalue);
      PlayerPrefs.GetInt("totalcoins", currentvalue);
      PlayerPrefs.Save();


please sir how to make this to increment once and save.

avatar image truedreams · Sep 19, 2017 at 01:05 PM 0
Share

Sir I debug it also . It executes again and again I mean the value comes from treetry get saves in variable and whenever I go to shop menu it increment the total value again and again . It debug result show the infinite loop is running . For example 12+12 = 24 . And if I go back to shop menu this will be 24 + 12 = 36 This is what happeing with me .

And debug.log while doing . It executes everytime whenever I go to shop menu

avatar image truedreams · Sep 19, 2017 at 01:06 PM 0
Share

please help me its been 1 month i am not able to get the soluton. please help !!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Sep 13, 2017 at 08:55 AM

HI !! @truedreams !

First, where you say

      currentvalue += currentvalue + gettingvalue;
      totalvalue += currentvalue + totalvalue;

i think you are not doing what you really want. The symbol += means "add to current value".

For example, if we have x=1 an do this:

 x += 3;

Now x = 4. But you are not doing this, you are increasing itself + another value. In your code, you are doing this:

  x += x+3;

So now x=5.

So.. i supose you wanted to do this

 currentvalue +=  gettingvalue;
 totalvalue +=  totalvalue;

I mean next 2 lines are exactly the same:

 x = x + 3;
 x += 3;

And if it increases only by one, next3 lines are the same:

 x = x+1;
 x += 1;
 x ++ ;

In other hand...

What program are you using for scripting?

Do you know how to debug the code while running the game

Debug means, the code is monitored, and you can mark code line, and when the game try to read and execute that lines, it stops, and allow you to check the state of your variables, to see if they are as its supposed they has to be. This allow you to check if a method is called, or how the variables changes.

If you are having problems operating with variables, debugging is your best friend! If need more help just ask concrete questions and we will help you !

Remember to Accept / Upvote the answers ! And reply using @tormentoarmagedoom

Bye! :D

Comment
Add comment · Show 7 · Share
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 truedreams · Sep 14, 2017 at 12:46 PM 0
Share

please read my comments above !

avatar image truedreams · Sep 15, 2017 at 11:41 AM 0
Share

Sir I am sorry to say that I am able to increment the shop coin menu coins now but it's incrementing again and again the same value in shop sorry sir but problem is still occuring it's incrementing again and again

How to increment it only once

avatar image truedreams · Sep 15, 2017 at 11:51 AM 0
Share
     this coding is able to increment coins but its incrementing  whenever i go to shop menu for example 15 + 15 = 30 and if i go back to shop menu this will be 30 + 15 because of contnious incrementation .
        
         gettingvalue = PlayerPrefs.GetInt("totalcoins", currentvalue) + treetry.instance.finalscore;
         PlayerPrefs.Save();
         currentvalue = currentvalue + gettingvalue;
         cointext.text = currentvalue.ToString();
         PlayerPrefs.SetInt("totalcoins", currentvalue);
         PlayerPrefs.GetInt("totalcoins", currentvalue);
         PlayerPrefs.Save();


please sir how to make this to increment once and save.

avatar image tormentoarmagedoom truedreams · Sep 18, 2017 at 02:27 PM 0
Share

$$anonymous$$Ake a Debug.Log after the increase, to check how many times is executing that line

avatar image truedreams tormentoarmagedoom · Sep 18, 2017 at 05:33 PM 0
Share

Sir I debug it also . It executes again and again I mean the value comes from treetry get saves in variable and whenever I go to shop menu it increment the total value again and again . It debug result show the infinite loop is running . For example 12+12 = 24 . And if I go back to shop menu this will be 24 + 12 = 36 This is what happeing with me .

And debug.log while doing . It executes everytime whenever I go to shop menu

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

69 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 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 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

How to solve a problem with UI anchors in Unity 5.2 0 Answers

Shadow bug after upgrading to 5.2 0 Answers

Native rendering plugin OpenGL device type not being set. 2 Answers

Create a Menu before starting AR App 1 Answer

Is it possible to have consistent development for Windows Universal and Android apps? 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