Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by DAlturG · Apr 19, 2015 at 11:32 PM · javascriptscripting problemshopcoin

coins go to 0

I want to be able to go to a shop scene from the main scene and have my coins the same in both of them but when i try to go to the shop scene the coins just turn to 0. Anyone know what the problem is? Heres my scripts:

Coins Script

 public int coin = 0;
 bool hit = false;
 public GUISkin myskin;

 void Start(){
     coin = PlayerPrefs.GetInt ("Coins",0);
 }
 
 
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "coin")
     {
         //coin ++;
         audio.Play();
         hit = true;
         
     }
 }
 void Update(){
             if (hit) {
                     coin ++;
                     hit = false;
             } 
     }

 void OnDisable()
 {
     PlayerPrefs.SetInt ("Coins", (int)coin);
     PlayerPrefs.Save ();
 }
 void OnGUI()
 { 
     
     GUI.skin = myskin;
     GUI.Box(new Rect(100,10,80,80), "");
     GUI.Label(new Rect(200,10,200,100), "" + coin);
 }

Shop Script

 public GUISkin myskin;
 int coin;
 int touch = 0;
 int time = 3;

 void start(){

     coin = PlayerPrefs.GetInt ("Coins",0);
 }
 void OnGUI(){
             GUI.skin = myskin;

     GUI.Label (new Rect (500,10,200,200), "Coins: " + (int)coin);

             if (GUI.Button (new Rect (Screen.width / 2 + 10, Screen.height / 2 + 2 * Screen.height / 5 + 8, Screen.width / 1 - 5, Screen.height / 3), "main")) {

                     Application.LoadLevel ("main");
             }
             if (GUI.Button (new Rect (100, 40, 100, 100), "shield")) {                   
             time = 5;
             coin += 100;

             }
             if (GUI.Button (new Rect (Screen.width / 4 + 10, Screen.height / 4 + 2 * Screen.height / 20, Screen.width / 2 - 10, Screen.height / 5), "Score x3")) {                        
                             
                 coin = coin - 10;
                     
             }
     }
 void OnDisable(){
     PlayerPrefs.SetInt ("Coins",(int)coin);
     PlayerPrefs.SetInt ("time", time);
     PlayerPrefs.Save();

 }
Comment
Add comment · Show 2
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 jessee03 · Apr 19, 2015 at 11:36 PM 0
Share

I would suggest using DontDestroyOnLoad. Yet from what I see you're not saving to PlayerPrefs before loading the scene. $$anonymous$$y suggestion would be to have one script that's constantly auto saving all your PlayerPrefs data. Unless you want to save manually.

avatar image DAlturG · Apr 20, 2015 at 06:38 PM 0
Share

Sorry, I'm new to unity. How could I make the script that could constantly auto save my PlayerPrefs data?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Apr 20, 2015 at 12:43 AM

You've got two scripts independently loading, updating, and saving the same variable to player prefs. This is bad practice, and probably why the value in player prefs keeps getting reset.

You'll want to load the value in just one of your scripts, and access it from the other script.

Comment
Add comment · Show 3 · 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 rockyourteeth · Apr 20, 2015 at 06:46 PM 1
Share

Agreed. What I've done in the past is create a "SaveData" script (or whatever) that is static, and create some functions on it that wrap the Player Prefs functionality that you want (basically identical functions that just call the player prefs function).

This is also useful because if you decide to save your data in another way later, besides player prefs (like using a cloud save system), then you've successfully already abstracted out the saving process, and will only have to change that script in one place.

avatar image DAlturG · Apr 20, 2015 at 06:51 PM 0
Share

could you show me how to make this script because i am fairly new to unity and dont understand how to make the saving process

avatar image DAlturG · Apr 20, 2015 at 08:58 PM 0
Share

Also could you tell me how i could save the coins so that the same number of coins would show if i turn the game off and if i was using the static functions?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shop system isn’t working and I don’t know why 1 Answer

Bullet bounce issue using Vector3.reflect 1 Answer

How to change button text in simple character shop ? 1 Answer

Endless Runner Coin Magnet Not working. 0 Answers

Problem with SUM 1 Answer


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