• 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 Agonir · Mar 04, 2017 at 09:56 AM · shader2d-platformervalue

Carrying coins/lives to next scene

Hello I'm trying to make a little 2D platform game and at the moment I have this code when I end the level:

 PlayerPrefs.SetInt ("countingCoins", theLevelManager.countingCoins); 
 PlayerPrefs.SetInt ("PlayerLives", theLevelManager.currentLives);  


I am using the getInt to get values and carry them over level but they keep adding so my currentlives goes to more than I intend,on my LevelManager script I use this code on void start();

 void Start () {
     thePlayer = FindObjectOfType<PlayerController> ();

     countHealth = maxHealth;

     whatToReset = FindObjectsOfType<ResetRespawn>();

 
     if (PlayerPrefs.HasKey ("countingCoins")) {
         countingCoins = PlayerPrefs.GetInt ("countingCoins");
         coinText.text = "Coins: " + countingCoins;
         }
         
     if (PlayerPrefs.HasKey ("PlayerLives")) {
         currentLives = PlayerPrefs.GetInt ("PlayerLives");
     } else {
             currentLives = startLives;
            }
     liveText.text = "Lives - " + currentLives;
 }

At the moment my countCoins adds up by ten and my currentLives keeps adding lives itself.

GIF of current lives changing value when I start game

I just want to carry the current values to next scene.

Any idea how can I fix this or where my code is wrong?

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 $$anonymous$$ · Mar 04, 2017 at 10:51 AM

Maybe the else statement is being set off cause you dont state what state the haskey bool has to be in for the if statement to pass..

 if (PlayerPrefs.HasKey ("PlayerLives")  == true) {
          currentLives = PlayerPrefs.GetInt ("PlayerLives");
      } else {
              currentLives = startLives;
             }
      liveText.text = "Lives - " + currentLives;
  }

https://docs.unity3d.com/ScriptReference/PlayerPrefs.HasKey.html

If that doesnt work, you can pinpoint the issue with debug log, Use the example ive given and watch console at runtime. This will show you whats really happening in the code

 if (PlayerPrefs.HasKey ("countingCoins")) {
 
          countingCoins = PlayerPrefs.GetInt ("countingCoins");
          Debug.Log("Counting Coins Exist, Coins = "+ countingCoins)
          coinText.text = "Coins: " + countingCoins;
          }
          
      if (PlayerPrefs.HasKey ("PlayerLives")) {
          currentLives = PlayerPrefs.GetInt ("PlayerLives");
          Debug.Log("PlayerLives Exsists, Lives = "+ countingCoins)
      } else {
              currentLives = startLives;
              Debug.Log("PlayerLives Doesnt Exsist, Lives = "+ countingCoins)
             }
      liveText.text = "Lives - " + currentLives;
  }
Comment
Add comment · Show 3 · 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 Agonir · Mar 04, 2017 at 10:57 AM 0
Share

I think there's no need for bool since .Has$$anonymous$$ey checks if the string "PlayerLives" exists and then executes the if

$$anonymous$$y problem is that the game keeps adding life and coins and I've no idea where from , I have this coRoutine at gameEnding scipt.

 public IEnumerator LevelEndCo() {
         zePlayer.can$$anonymous$$ove = false;
         theCamera.followTarg = false;
 
         theLevel$$anonymous$$anager.level$$anonymous$$usic.Stop ();
         theLevel$$anonymous$$anager.gameOver$$anonymous$$usic.Play ();
 
         zePlayer.playerBody.velocity = Vector3.zero;
 
         PlayerPrefs.SetInt ("CointCount", theLevel$$anonymous$$anager.countingCoins); 
         PlayerPrefs.SetInt ("PlayerLives", theLevel$$anonymous$$anager.currentLives); 
         PlayerPrefs.SetInt (levelToUnlock, 1);
 
         yield return new WaitForSeconds (waitTo$$anonymous$$ove);
         movePlayer = true;
 
         yield return new WaitForSeconds (waitToLoad);
 
         Scene$$anonymous$$anager.LoadScene (levelToLoad);
 
         Debug.Log ("Level ended succesfully");
     }
 }

avatar image $$anonymous$$ Agonir · Mar 04, 2017 at 11:02 AM 0
Share

Try the second half... See what shows up in console if this is the only point where points and coins could be having abnormalities

avatar image Agonir $$anonymous$$ · Mar 04, 2017 at 03:54 PM 0
Share

Here's a gif,I clearly have 0 Coins as start and 3 lives but somehow it starts with 60 coins and 8 lives??

GIF

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

120 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

Related Questions

Would like to know how expensive is SpriteRenderer.color as I am planning to use it to tint all of the sprites on my screen? 0 Answers

Grass wave shader or other ideas 2 Answers

Shader Graph 2D(v.2019): How can create Transparency Gradient on shader plain? 0 Answers

Help with grabpass shader for 2D water effect. 2 Answers

Camera replacement shader not working 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