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 Matt_2001 · Jan 19, 2021 at 03:31 PM · uiunity 2dplayerprefsdatabasesave data

How can I save my game data

Hi, how can I save the gamedata of my shop. I am storing the score and other numerical value or strings in playerprefs but it came to know that player prefs can only store some limited values and I want to save my shop data. In my game when the user purchase something by clicking buy the buy button changes to equip button but if the user exit the game and play again the buy button is activated again thats what I want to save. I want to prevent that somehow how can I do that.

script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using UnityEngine.EventSystems;

public class shop : MonoBehaviour { public TMPro.TextMeshProUGUI scoreText; public GameObject Item1; public GameObject Item2; public GameObject Item3; public GameObject Item4; public TMPro.TextMeshProUGUI notenough; public TMPro.TextMeshProUGUI notenough1; public TMPro.TextMeshProUGUI notenough2; public TMPro.TextMeshProUGUI notenough3; public GameObject buyButton; public GameObject buyButton1; public GameObject buyButton2; public GameObject buyButton3; public GameObject equipButton; public GameObject equipButton1; public GameObject equipButton2; public GameObject equipButton3; public float sec = 14f;

private Dictionary ItemPrices;

void Start () { scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();

ItemPrices = new Dictionary() { { Item1, 50f }, { Item2, 80f }, {Item3, 3500f}, { Item4, 5000f }, }; notenough.enabled = false; notenough1.enabled = false; notenough2.enabled = false; notenough3.enabled = false; }

public void PurchaseItem(GameObject Item) { foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {

       float price = item.Value;
       float val = PlayerPrefs.GetFloat ("Highscore"); 
       if(val >= price)
       {
       val -= item.Value;
        PlayerPrefs.SetFloat("Highscore", val);
        scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
            buyButton.SetActive(false);
            equipButton.SetActive(true);
        
           // Take away the cost of the item from the player's currency
           //PlayerPrefs.GetFloat ("Highscore") -= item.Value;
       }
       else
       {
           Debug.Log("not enough money");
            notenough.enabled = true;
            notenough1.enabled = true;
            notenough2.enabled = true;
            notenough3.enabled = true;
           Invoke("noen", 2);
       }
      }     
 }

}

public void PurchaseItem1(GameObject Item) { foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {

       float price = item.Value;
       float val = PlayerPrefs.GetFloat ("Highscore"); 
       if(val >= price)
       {
       val -= item.Value;
        PlayerPrefs.SetFloat("Highscore", val);
        scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
            buyButton1.SetActive(false);
            equipButton1.SetActive(true);
           // Take away the cost of the item from the player's currency
           //PlayerPrefs.GetFloat ("Highscore") -= item.Value;
       }
       else
       {
           Debug.Log("not enough money");
            notenough.enabled = true;
            notenough1.enabled = true;
            notenough2.enabled = true;
            notenough3.enabled = true;
           Invoke("noen", 2);
       }
      }     
 }

}

public void PurchaseItem2(GameObject Item) { foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {

       float price = item.Value;
       float val = PlayerPrefs.GetFloat ("Highscore"); 
       if(val >= price)
       {
       val -= item.Value;
        PlayerPrefs.SetFloat("Highscore", val);
        scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
            buyButton2.SetActive(false);
            equipButton2.SetActive(true);
           // Take away the cost of the item from the player's currency
           //PlayerPrefs.GetFloat ("Highscore") -= item.Value;
       }
       else
       {
           Debug.Log("not enough money");
            notenough.enabled = true;
            notenough1.enabled = true;
            notenough2.enabled = true;
            notenough3.enabled = true;
           Invoke("noen", 2);
       }
      }     
 }

}

public void PurchaseItem3(GameObject Item) { foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {

       float price = item.Value;
       float val = PlayerPrefs.GetFloat ("Highscore"); 
       if(val >= price)
       {
       val -= item.Value;
        PlayerPrefs.SetFloat("Highscore", val);
        scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
         buyButton3.SetActive(false);
            equipButton3.SetActive(true);
           // Take away the cost of the item from the player's currency
           //PlayerPrefs.GetFloat ("Highscore") -= item.Value;
       }
       else
       {
           Debug.Log("not enough money");
            notenough.enabled = true;
            notenough1.enabled = true;
            notenough2.enabled = true;
            notenough3.enabled = true;
           Invoke("noen", 2);
       }
      }     
 }

}

/*public void buy() {

if (val >= price) { PurchaseItem(); } else {

}*/

public void noen() { notenough.enabled = false; notenough1.enabled = false; notenough2.enabled = false; notenough3.enabled = false;

}

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 Duckocide · Jan 19, 2021 at 04:58 PM

This is a good place to start - https://learn.unity.com/tutorial/overview-and-serialization-concepts

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

203 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

Related Questions

it is Playerprefs can use in saving highscore and both PC having this can same see data in Highscores? 0 Answers

Unity problem when i convert playerprefs save to the standard file io save 0 Answers

Name not saving to or loading from PlayerPrefs 1 Answer

[Android] Save data loss with PlayerPrefs? 2 Answers

How to Save Object (Button) information on game restarting? please check code. 2 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