• 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 Alter · Mar 05, 2013 at 11:14 AM · c#playerprefsleveling

Leveling UP/ Experience system using PlayerPrefs

Heya guys, I almost have an idea on getting an experience system down using PlayerPrefs. Only trying to figure out how I would apply it to multiple characters not just the one.

I have it set as each time I kill an enemy I gain 10 exp, but unsure on how to make it not affect a separate character that starts on level 1.

         int experience = PlayerPrefs.GetInt("Experience", exp);
         void Update()
     {
         if(enemyHeath =< 1)
         {   
         PlayerPrefs.SetInt("Experience",PlayerPrefs.GetInt("Experience")+10);
         }
     
     if (PlayerPrefs.GetInt("Experience") > 500)
     
        {
           PlayerPrefs.SetInt("SavedLevelUp1", 500);
        }


Any idea please help and thank you in advance.

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

Answer by Statement · Mar 06, 2013 at 02:47 PM

Hi, while it can be used for arbitrary storage, it isn't really intended for savegame features as it is limited and very simple.

From docs:

Stores and accesses player preferences between game sessions.

Anyhow, if you still want to go on with it, I presume your issue is that you don't know how to store experience for player 1 and for player 2 and so on..

You could have

 // How many players exist here?
 int numPlayers = PlayerPrefs.GetInt("NumberOfPlayer", 0);
 // Select which player should play (or possibly create a new one)
 int player = SelectAPlayerToPlayWith(numPlayers);

 // Get the player specific pref keys
 string keyExperience = string.Format("Player{0}.Experience", player);
 string keySavedLevelUp1 = string.Format("Player{0}.SavedLevelUp1", player);

 // ... your existing code with some modifications
 int experience = PlayerPrefs.GetInt(playerExperience, exp);
 void Update()
 {
     if(enemyHeath =< 1)
         PlayerPrefs.SetInt(keyExperience, PlayerPrefs.GetInt(keyExperience) + 10);
     if (PlayerPrefs.GetInt(keyExperience) > 500)
         PlayerPrefs.SetInt(keySavedLevelUp1, 500);
 
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 Alter · Mar 07, 2013 at 11:58 AM 0
Share

Cheers, Statement. It's not so much as a player(s) but characters themselves, in the game I'm designing there is 9 unlock-able characters. So that being said would that apply for the "NumberOfPlayer"? sorry guys, I'm trying to get my head around it but it's still not clicking as fast as I want it too.

An Idea that came to my head was that, since each character has a different weapon I was thinking of something like:

      if(other.gameObject.tag =="Sword")
             {
                 PlayerPrefs.SetInt("$$anonymous$$nightExperience" + 10);
                 Debug.Log("Gain 10exp");
                 //
     
             }
 
      if(other.gameObject.tag =="Dagger")
             {
                 PlayerPrefs.SetInt("RogueExperience" + 10);
                 Debug.Log("Gain 10exp");
                 //
     
             }

Then apply this script to each character with a 'prefs' script with;

 knightLevelReached = PlayerPrefs.GetInt("$$anonymous$$nightExperience");
 
 public int knightLevelReached = 0;
 
 void Update()
 {
 if(knightLevelReached > 100)
 {
    Debug.Log("Reached Level 2")
 }
 }

Any thoughts?

avatar image
0

Answer by poncho · Mar 05, 2013 at 03:19 PM

Use a variable to Have your level, "PlayerLevel", and use a math formula to have the experience required per level, 1st level 10exp, 2nd level 25exp, 3rd lvl 45 exp, etc..., so now your experience validation would be

 if(experience > ExperienceOnLevel(Playerprefs.GetInt(PlayerLevel)))
 {
 //add a level to playerlevel, and add stats according to it
 }

good luck and happy coding

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 Alter · Mar 06, 2013 at 01:14 PM 0
Share

Thank you for the reply. But I'm not sure if I understand this, but pretty much just replace the exp code with what you have given me? Also how would this work on separate characters? Sorry, I'm new to programming and have just been working on level unlocks which effects the whole game and the same with coins collected. ^^

avatar image poncho · Mar 06, 2013 at 02:15 PM 0
Share

no, use the logic of having Independent variable for your characters, PlayerLevel1, PlayerLevel2,... and also having ExpPlayer1, ExpPlayer2,... the ExperienceOnLevel $$anonymous$$ethod, should return your calculated exp per level that in case your experience is above that amount, then your player will get a levelup, for the new in programming, i would recommend, c# for dummies, GREAT book to learn from start. happy coding

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

UI Slider, Save Values and Change Them 0 Answers

playerprefs and UI. 3 Answers

Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges