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 /
  • Help Room /
avatar image
0
Question by ohkayem · Jan 17, 2018 at 07:29 PM · playerprefslevel

Player Prefs Issue

Dear Coders I'm having a lot of trouble with this.

I have created the following scripts :

1) A script that saves stats like health, ammo, clips in a home screen

2) An in-game script in which a player can collect game objects to boost ammo and clips

3) An XP script for the player to level up

My scripts generally work perfectly for increasing maximum ammo and maximum clips. These save between my two scenes.

I can also increase my current ammo and current clips but this is where the issue starts.

For some reason, whilst in game health, ammo and clips increase until their max values have been reached. These figures save in the home screen. But when going back into the game scene, ammo and clips values reduce.

Strangely this drop in values in current ammo and current clips seems to correlate with the increases as the player is levelling up.

So, for example if maxClips = maxClips + 2, maxClips will ALWAYS increase by 2 on levelling up and I can collect currentClips to the defined maxClips value in the game at that time, save this value into the home screen but then going back into the game scene it will always drop to 4.

If maxClips = maxClips + 3, my currentClips will only go up to 6 if I exit out of the game scene and then enter back in and so forth.

I'm having exactly the same issue with current health/max health and current ammo/ max ammo.

Scripts are as follows :

Firing Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI; 
 
 public class PlayerFire : MonoBehaviour {
 
         public int maxClips; 
         public int minClips; 
         public int currentClips;  
 
         
     void Start () {
 
             maxClips = 0;
 
             currentAmmo = PlayerPrefs.GetInt ("currentAmmo");
             currentClips = PlayerPrefs.GetInt ("currentClips");
         }
 
         void Update (){
         
             PlayerPrefs.SetInt ("currentClips", currentClips);
             PlayerPrefs.SetInt ("maxClips", maxClips);
 
             PlayerPrefs.SetInt ("maxAmmo", maxAmmo);
             PlayerPrefs.SetInt ("currentAmmo", currentAmmo);
 
             PlayerPrefs.Save ();
     
             if (currentClips >= maxClips) {
                 currentClips = maxClips;
             }
 
             if (currentClips < minClips) {
                 currentClips = minClips; 
         }
         }
 }
 

Levelling Up Script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI; 
 
 public class PlayerXP : MonoBehaviour {
     PlayerFire playerFire; 
 
     public int currentLevel;
     public int currentXP;
     public int maxXP; 
     public int minXP;
     public int xpToLevelUp;
     public int [] toLevelUp;
 
     void Start () {
         
         currentXP = PlayerPrefs.GetInt ("playerXP");
 
         minXP = 0;
 
         playerFire = GetComponent<PlayerFire> (); 
         
         } 
     }
 
     void Update (){
         
         PlayerPrefs.SetInt ("playerXP", currentXP); 
         PlayerPrefs.SetInt ("maxXP", maxXP = 900);
 
         xpToLevelUp = toLevelUp[currentLevel];
         if (currentXP >= xpToLevelUp) { 
             LevellingUp (); 
         }
 
         LevellingUp ();
     }
 
     public void LevellingUp(){
         
         if (currentXP >= toLevelUp [currentLevel]) {
 
             StartCoroutine (upgradingPlayer ()); 
 
             currentLevel++; 
         
             playerFire.maxClips = playerFire.maxClips + 1;
             playerFire.maxAmmo = playerFire.maxAmmo + 10; }
             }
    }

Home screen script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI; 
 using UnityEngine.SceneManagement; 
 
 public class EnterItemCollectLevel : MonoBehaviour {
     public int maxAmmo;
     public int currentAmmo; 
     public int maxClips;
     public int currentClips; 
 
 
     void Start (){
         
         currentAmmo = PlayerPrefs.GetInt ("currentAmmo");    
         maxAmmo = PlayerPrefs.GetInt ("maxAmmo");
 
         currentClips = PlayerPrefs.GetInt("currentClips"); 
         maxClips = PlayerPrefs.GetInt ("maxClips");
     }
         
     void Update ()
     {
         if (Input.GetKey (KeyCode.I)) {
             SceneManager.LoadScene (2);
         }
 }
             

I appreciate that this may be slightly confusing but any help that anyone could offer would be very much appreciated - I'm tearing my hair out here!

Thankyou 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

0 Replies

· Add your reply
  • Sort: 

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

75 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

Related Questions

PlayerPref doesnt work 2 Answers

Unlocked levels Lock if a previous level played 0 Answers

Level Unlock System....frustrated,Building a level unlock system using player prefs 0 Answers

PlayerPrefs Not Saving When I Click the Button. 1 Answer

PlayerPrefs works fine in Unity Editor but not on Android 0 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