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 aminkhosravi007 · Apr 14, 2020 at 08:17 AM · unity 5uisaveload

Playerprefs to Save UI TEXT

hello everyone. I have two variables in my game. One is for Score and the other is for coin that's defined through UI Text. I want to save number of coins collecting by player. I failed to save and load. here is my script;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class UIManager2 : MonoBehaviour {
 
     public static int coin_score = 0;
 
     public Text coin_text;
 
 
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
 
         coin_text.text = coin_score.ToString ();
 
     }
 }
 
 
 
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by azerty0220pl · Apr 14, 2020 at 08:57 AM

Something like this should work:

  void Start()
 {
  coin_score = PlayerPrefs.GetInt("anyNameYouLike") //Load value
 }
 
 void Update()
 {
  coin_text.text = coin_score.ToString();
  PlayerPrefs.SetInt("anyNameYouLike", coin_score); //Save value
 }

You can save a string (PlayerPrefs.SetString("name", stringToSave)), but I do not recommend it in this case. Saving the int is much easier. Also I would recommend updating the score only when it is modified.

Comment
Add comment · Show 2 · 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 aminkhosravi007 · Apr 14, 2020 at 09:45 AM 0
Share

Thanks a lot. It worked. but could you please help me in another issue? I have a $$anonymous$$enu in which buttons represent levels and characters. I want to say that if the player has for example 5 coins, That character or level must be unlocked and the piece of wood that acts as a price tag disappear. Every thing worked fine but when i return to my main menu and tried to reload the game, every thing restarted to default. How can I save unlocked stuff?

avatar image azerty0220pl aminkhosravi007 · Apr 14, 2020 at 02:44 PM 0
Share

You can make a variable for each product which will store the status of the product. In the Start() function load the values and depending on their values, unlock the item bought, and save the variable after purchasing a product.

avatar image
0

Answer by aminkhosravi007 · Apr 14, 2020 at 10:44 AM

I'm new in unity Your code worked fine but when I try to buy a new item, My coin number doesn't change. I wrote a code in which for any item to buy, number of coins must be decreased. It worked fine before, But now It fails. Should I put your code in Awake ()? @azerty0220pl

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 azerty0220pl · Apr 14, 2020 at 02:40 PM 0
Share

You can put my code in Awake(). However I would make it easier by putting it in the Start() function or even making a new function will be probably better, something like this:

 void Start()
 {
  CoinNumberUpdate(); //Calling custom function
 }
 
 public void CoinNumberUpdate()
 {
  coin_score = PlayerPrefs.GetInt("anyNameYouLike");
  coin_text.text = coin_score.ToString();
 }

And remember to call that function everytime coin_score is changed and saved. For example, if you buy a new product using other script in other gameObject:

 public GameObject prevScriptObject; //GameObject to which the previous script is attached to
 int prize = 100;
 int coin_score;
 
 public void purchase() //Function called by the buy button
 {
 coin_score = PlayerPrefs.GetInt("anyNameYouLike"); //First load coin_score
 
  if(prize <= coin_score) //Condition
  {
   coin_score = coin_score - prize; //New coin_score value
   PlayerPrefs.SetInt("anyNameYouLike", coin_score); //Save before updating the text on screen
   prevScriptObject.GetComponent<PrevScriptName>().CoinNumberUpdate();
  }
  else
  {
    //If not suficient money, do something...
  }
 }

avatar image aminkhosravi007 · Apr 14, 2020 at 05:41 PM 0
Share

thanks for your time! I appreciate that. I know your code is fine. as my menu is in another scene, I created an empty GameObject and attached my UI$$anonymous$$anager2. renamed it to for example "Reference" and started writing my code. I wrote a code in which for every 40 points, my coins must be increased for 1 unit. For your first code, I think coin_text.text = coin_score.ToString(); must be in Update() because it didn't work. here is my second script in which when player clicks on the button (WhitePlane) something happens

 using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.Scene$$anonymous$$anagement;
     using UnityEngine.UI;
     public class $$anonymous$$ain$$anonymous$$enu3 : $$anonymous$$onoBehaviour {
     
         
     
     
         public Button WhitePlane;
             public GameObject Reference;
             public static int character_number;
     
     WhitePlane.onClick.AddListener (() => {
                 
                             character_number=2;
     
                 if (UI$$anonymous$$anager2.coin_score>=1){
                     UI$$anonymous$$anager2.coin_score--;
                                Scene$$anonymous$$anager.LoadScene ("$$anonymous$$enu2");
                                     }
                     });

I tried your codes but I thinks I must have missed one step. Could you please write your second code into my second script? @azerty0220pl

avatar image aminkhosravi007 · Apr 15, 2020 at 06:59 AM 0
Share

thanks I fixed it!

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

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

How do I save and load the value of a text? 1 Answer

Unity Won't Load 0 Answers

Coding help: How to use xml serialization 1 Answer

Unity - keep created buttons after quit 1 Answer

How to save a prefab when a button is pressed 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