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
1
Question by HHameline · Apr 28, 2011 at 07:56 PM · playerprefsinventory

Using PlayerPrefs to Imitate a players inventory

Hello, I don't really have any code yet, because to be honest I don't really know where to start. I was hoping that someone could actually point me to a good resource for how PlayerPrefs worked. Also if anyone would be willing to past a little bit of pseudocode or something so I can understand the basics of PlayerPrefs that would be great. What I want to do is imitate a players inventory and when the player clicks on the shop the items the player already has in their inventory will not display in the shop. But since I don't have the database set up yet i thought PlayerPrefs would be the way to do it does that sound about right?

Thanks, Hans Unity Newb

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
3
Best Answer

Answer by Bumbaz 1 · Apr 28, 2011 at 09:16 PM

The PlayerPrefs class stores information directly to your hard drive. There are two main types of functions, Set and Get. These are divided into SetInt, GetInt, SetFloat, GetFloat, SetString, GetString, depending on the type of variable you want to store.

Let's say I want to store to the disk that the player has reached a score of 150. To store a certain variable to the hard disk, you can do this:

PlayerPrefs.SetInt("Score", 150);

Note that the function takes two parameters. The first is the key, and it is used to identify the information stored at a later time.

Later in the game, I want to be able to recall the score that the player has achieved earlier, and print it to the console. To do this I'm going to use the GetInt function.

print(PlayerPrefs.GetInt("Score"));

This will effectively print out 150 to the console. The same method works with floats and strings:

PlayerPrefs.SetString("My favourite fruit", "Pineapple");

print("My favourite fruit is " + PlayerPrefs.GetString("My favourite fruit"));

PlayerPrefs.SetFloat("Pi", "3,14159265");

print("The value of Pi is " + PlayerPrefs.GetString("Pi"));

Using this method you can store information easily such as currency or items, but doing so will probably be very tedious if you have a lot of items. In any case, let's say your character buys an item from a NPC, you'll want to do something like this:

PlayerPrefs.SetString("Powerful Weapon",  "Powerful Weapon");

To later check if the player has this item in the inventory, you can use another PlayerPrefs function, PlayerPrefs.HasKey. This will return true if the key exists in the preferences.

if(PlayerPrefs.HasKey("Powerful Weapon")) {

//Hide item from shop

}

To delete an item from your inventory, you can use:

PlayerPrefs.DeleteKey("Useless Weapon");

To delete every item in your preferences:

PlayerPrefs.DeleteAll();

Hope I helped.

Comment
Add comment · Show 1 · 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 HHameline · Apr 29, 2011 at 12:01 PM 0
Share

Awesome! Thanks a lot this will be very handy indeed. Gotta love Unity!

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

No one has followed this question yet.

Related Questions

Need experienced opinion: Saving a players inventory: XML or PlayerPrefs? 1 Answer

How to load an inventory array? 1 Answer

PlayerPrefs Lmitations 2 Answers

I am having trouble getting a web build to save out an .upp file from my code 0 Answers

Check the size of PlayerPrefs from web player 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