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 /
avatar image
0
Question by gpuelston2004 · Feb 10, 2019 at 05:25 AM · scripting problemplayerprefs

My playerprefs wont save and load my int to TMPro text

 using System.Collections;
 using System.Collections.Generic;
 using TMPro;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 public class Currency : MonoBehaviour {
 
     public int tokens;
     public int index;
     GameObject currencyUI;
     GameObject currencyU;
     void Start()
     {
         currencyUI = GameObject.Find("Currency");
         currencyU = GameObject.Find("Currency");
 
 
 
         currencyUI.GetComponent<TextMeshProUGUI>().text = PlayerPrefs.GetInt("Tokens", 0).ToString();
     }
 
     // Update is called once per frame
     void Update()
     {
         currencyUI.GetComponent <TextMeshProUGUI> ().text = tokens.ToString();
         if (tokens < 0)
         {
             tokens = 0;
         }
 
         PlayerPrefs.SetInt("Tokens", tokens);
 
     }
 
 }

This is the first script that makes it so that the currency is actually going up and being put on the game then the next script is meant to allow it so when you pick the coins up it adds it to the players token count but iv been trying for hours on end to make the script so that i can save it using something possibly even playerprefs but it just dosent seem to be working

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 public class PickupMoney : MonoBehaviour
 {
     Currency script;
     private GameObject currencyU;
     public int addAmount;
 
     void Start()
     { 
         script = GameObject.FindWithTag("GameController").GetComponent<Currency>();
         currencyU = GameObject.Find("Currency");
         //Currency saver \/
         currencyU.GetComponent<TextMeshProUGUI>().text = PlayerPrefs.GetInt("Tokens", 0).ToString();
         PlayerPrefs.SetInt("Tokens", script.tokens);
     }
 
 
 
 
     private void Update()
     {
 
 
        // currencyU.GetComponent<TextMeshProUGUI>().text = PlayerPrefs.GetString("tokens");
 
         //PlayerPrefs.GetString("tokens", currencyU.GetComponent<TextMeshProUGUI>().text);
 
         PlayerPrefs.SetInt("Tokens", script.tokens);
         currencyU.GetComponent<TextMeshProUGUI>().text = PlayerPrefs.GetInt("Tokens", 0).ToString();
         PlayerPrefs.Save();
 
 
     }
 
 
     void OnTriggerEnter(Collider obj)
     {
         if (obj.gameObject.tag == "Player")
         {
             script.tokens += addAmount;
             Destroy(gameObject);
 
             //Currency saver \/
 
         }
     }
 }

if anyone knows the problem or wants to help but needs more information please feel free to ask any help is appreciated thanks for everyone help and time :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RadonRaph · Feb 10, 2019 at 11:42 AM

Hello @gpuelston2004, i thin you should try somethign like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 public class Currency : MonoBehaviour {
     public int tokens;
     GameObject currencyUI;
 
     // Use this for initialization
     void Start () {
         //We set the var tokens with the amount of tokens from previous sessions
         tokens = PlayerPrefs.GetInt("Tokens", 0);
         currencyUI = GameObject.Find("Currency");
     }
     
     // Update is called once per frame
     void Update () {
         currencyUI.GetComponent<TextMeshProUGUI>().text = tokens.ToString();
         if (tokens < 0)
         {
             tokens = 0;
         }
     }
 
     public void AddTokens(int amount)
     {
         tokens += amount;
         PlayerPrefs.SetInt("Tokens", tokens);
         PlayerPrefs.Save();
     }
 }
 
 public class PickUpMoney : MonoBehaviour
 {
     GameObject CurrencyManager;
     Currency currency;
 
     public int addAmount;
 
     void Start()
     {
         CurrencyManager = GameObject.Find("CurrencyManager");
         currency = CurrencyManager.GetComponent<Currency>();
     }
 
 
     void OnTriggerEnter(Collider obj)
     {
         if (obj.gameObject.tag == "Player")
         {
             currency.AddTokens(addAmount);
             Destroy(gameObject);
         }
     }
 }
 


All your tokens are stored in a var named tokens in the "Currency" script. At each start the tokens's var initialize with the previous value of previous session. If you want to modifiy the amount of tokens create a public function that modify the amount and save it in the playerPrefs.

Comment
Add comment · 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

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

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

Script with PlayerPrefs is not working on a different computer 1 Answer

Unity Character Selection + store 1 Answer

Destroy and Object and PlayerPrefs 1 Answer

How to stop objects you picked up from reappearing when you go to another level and return 1 Answer

How do I get my endless runner high score,How do I make high score for an endless runner 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