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 BlazerLazer · Mar 30, 2018 at 06:27 PM · sceneplayerprefsplayerpref

problem upgrading with playerprefs.

I have a racing game and I'm trying to make upgrades. once you beat a level you haven't beat before, it gives you an upgrade point you can use to increase your speed or handling. the problem is that it gives me a point like normal when I win, but it gets set to zero when I go back to the start menu. keep in mind that both my level scene and my level select scene use the same code I used two codes for this so it might be very complicated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class WinLevel : MonoBehaviour {
 
     public Text WinText;
     public Button WinButton;
     public Text ButtonText;
     public string LevelUnlocked = "Level2";
     public int LevelToUnlock = 2;
     int LevelReached = PlayerPrefs.GetInt("LevelReached");
     public int UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
     public Button[] levelButtons;
     public Text[] levelTexts;
     public Text[] ButtonTexts;
     private bool IsFinished = false;
 
     // Use this for initialization
     void Start()
     {
         LevelReached = PlayerPrefs.GetInt("LevelReached");
 
         UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
 
         Debug.Log("Points: " + UpgradePoints);
 
         for (int i = 0; i < levelButtons.Length; i++)
         {
             LevelReached = PlayerPrefs.GetInt("LevelReached");
 
             if (i + 1 > LevelReached)
             {
                 levelButtons[i].enabled = false;
                 levelTexts[i].enabled = false;
                 ButtonTexts[i].enabled = false;
             }
         }
         Debug.Log("LevelReached is " + LevelReached);
         Debug.Log("LevelToUnlock is " + LevelToUnlock);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("FinishLine"))
         {
             WinText.enabled = true;
             WinButton.enabled = true;
             ButtonText.enabled = true;
 
             if ( LevelToUnlock > LevelReached && !IsFinished)
             {
                 PlayerPrefs.SetInt("LevelReached", LevelToUnlock);
                 Debug.Log("Level Unlocked!");
                 PlayerPrefs.SetInt("UpgradePoints", UpgradePoints++);
                 Debug.Log("Points: " + UpgradePoints);
             }
             IsFinished = true;
         }
     }
     private void Update()
     {
         
     }
 }
 

as well as that code, I'm also using this code for using the upgrades

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Upgrades : MonoBehaviour {
 
     public int UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
     public int Speed = PlayerPrefs.GetInt("Speed", -400);
     public int Handling = PlayerPrefs.GetInt("Handling");
     public int TurnSpeed = PlayerPrefs.GetInt("TurnSpeed");
     public int Boosts = PlayerPrefs.GetInt("Boosts");
 
     public Text PointsText;
     public AudioSource Sound;
 
     // Use this for initialization
     void Start () {
 
         UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
         Speed = PlayerPrefs.GetInt("Speed");
         TurnSpeed = PlayerPrefs.GetInt("TurnSpeed");
         PointsText.text = "Points: " + UpgradePoints;
         Debug.Log("Points: " + UpgradePoints);
     }
     
     // Update is called once per frame
     void Update () {
         UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
     }
 
     public void SpeedUpgrade(int su)
     {
         if (UpgradePoints > 0)
         {
             Sound.Play();
             PlayerPrefs.SetInt("Speed", Speed - 50);
             PlayerPrefs.SetInt("UpgradePoints", UpgradePoints--);
             UpgradePoints = UpgradePoints--;
             PointsText.text = "Points: " + UpgradePoints;
             Speed = PlayerPrefs.GetInt("Speed");
             Debug.Log("Speed: " + Speed);
         }
     }
 
     public void HandlingUpgrade(int hu)
     {
         if (UpgradePoints > 0)
         {
             PlayerPrefs.SetInt("UpgradePoints", UpgradePoints--);
             PointsText.text = "Points: " + UpgradePoints;
             Sound.Play();
         }
     }
 
     public void TurnSpeedUpgrade(int tu)
     {
         if (UpgradePoints > 0)
         {
             PlayerPrefs.SetInt("UpgradePoints", UpgradePoints--);
             PointsText.text = "Points: " + UpgradePoints;
             PlayerPrefs.SetInt("TurnSpeed", TurnSpeed + 5);
             TurnSpeed = PlayerPrefs.GetInt("TurnSpeed");
             Sound.Play();
             Debug.Log("TurnSpeed: " + TurnSpeed);
         }
     }
 
     public void BoostUpgrade(int bu)
     {
         if (UpgradePoints > 0)
         {
             PlayerPrefs.SetInt("UpgradePoints", UpgradePoints--);
             PointsText.text = "Points: " + UpgradePoints;
             Sound.Play();
         }
     }
 }
 

don't mind all that other stuff. I'm just focusing on the playerprefs when I start a scene. what here might cause a playerpref to go back to zero when the scene is loaded?

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

90 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

Related Questions

Problems with pulling a slider value - (Making sensitivity Sliders) 0 Answers

What for are PlayerPrefs with 2 or more Scenes? 1 Answer

I'm trying to set a high score but I can't display it in another scene? 2 Answers

Playerprefs across Scenes 1 Answer

How to pass array variables from one script in one scene to another script in another scene? 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