Search Unity

Question Unity3d: How to save and bring score to next game level?

Discussion in 'Editor & General Support' started by Unity-Student0, Jan 26, 2023.

  1. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    Hello,

    I've been checking the internet and I understand I'm supposed to use PlayerPrefs. However, after taking the code from https://stackoverflow.com/questions/54062554/how-can-i-retain-my-player-score-to-a-new-scene and using it in

    Code (CSharp):
    1. CurrentScore = txtScore.text;
    2. PlayerPrefs.SetInt("CurrentScore", score);
    3. print("CurrentScore : ", CurrentScore);
    I get the error "Code not properly implemented" when playing it. Everything else about my game works perfectly. Could someone give step by step?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,694
    Steps to success:

    - create a GameManager (see below for a complete example)

    - put your integer score value there

    - update the score value as required
    ---> zero it at start of game
    ---> increment it as you score
    ---> display it when it changes

    Full step-by-step integration notes included below:

    ULTRA-simple static solution to a GameManager:

    https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b

    https://forum.unity.com/threads/i-need-to-save-the-score-when-the-scene-resets.1168766/#post-7488068
     
  3. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    Kurt, I don't understand your example. Do I just paste the file to my project and watch it work? Can you give more descriptive steps? In the meantime, I will check other projects I have to understand this further.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,694
    No, this is software engineering, not baking cookies.

    You read a description of a solution and you understand the flow of the data.

    Then you go to your context (game) and apply the knowledge to achieve a successful operation on your data.
     
  5. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    I don't know how to access the GM and GMshow scores from other scripts. That's one thing I haven't learnt yet. Now can you help or are you going to just say figure the logic? Let's see what reply you give.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,694
    You have a LOOOOOOOONG track record here of simply failing to read what we suggest you read.

    How about instead you prove to us here on the studio that you actually READ AND ATTEMPTED the detailed step-by-step integration instructions below the gist by copying them here and telling us what happened as you did each step?
     
  7. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    I have re-read and i now understand what you are showing. However, I don't want to create another 2 files. I want my playerprefs to be IN my player file. I will check my older projects and hope someone else can contribute. Your method may work but it's not what I want.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,694
    Oh no, my method absolutely works. Source: I use it all the time and so does my company.

    Remember, I do this for a living.

    Are you trolling us now? Or are you being serious that you don't want 2 files?

    I will pick my lower jaw up off the ground and eagerly await your explanation.
     
  9. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    Kurt, this isn't about making fun or insulting you. This is about making the playerprefs independent. It's as simple as that. I just opened my previous projects and found the code. I'll play around with it and see how it works. And if it does work, it means your files are not as efficient as I thought. You are soooo easily insulted.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,694
    You are officially invited to submit a more efficient version of my files.

    Again, waiting.
     
  11. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    Kurt, stop telling me what to do. That's MY prerogative. Without sounding discourteous or disrespectful, my decision is not your business to decide.
     
    Kurt-Dekker likes this.
  12. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    I don't know where you got that information from, but you do not need to use PlayerPrefs to allow a different scene to access your score.
    All you need is a static class (which in Kurt's very first post he called a GameManager), and in that put all the variables that you want to access from any scene - you just need these to be static.
    Having the class as static will mean you don't attach it to any GameObjects in your project/scenes, and you can then just reference those static variables from any script where you want them.
    Not sure what your fixation with PlayerPrefs is, but you would only really need to write and read that for persisting values between game sessions (ie: each time you start and end the game). If you only want the values persisting within the single game session, then you can just use the static class and variables route.
     
    Ruslank100 likes this.
  13. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    I don't understand you. Is PlayerPrefs a more common method or just a differerent alternative? I just checked the internet and found another video showing what i need. My original project just shows the concept. If I really succeed, this thread will really end. Is Kurt's method easier?
     
  14. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    As Babia said, PlayerPrefs is usually used to store persistent data: information that sticks around between gameplay sessions.

    It is perfectly usable to save data between scenes, but it's not necessary.

    If all you want to do is save a number in one scene and remember it in another scene, then static variables will work fine.

    Code (CSharp):
    1. public class ScoreTracker {
    2.   public static int score;
    3. }
    Really, that's it :p
     
  15. Unity-Student0

    Unity-Student0

    Joined:
    Aug 10, 2020
    Posts:
    263
    OK but how do you recall it in scene 2? Can you give more information? If you can help solve this, it can save me the time from checking youtube videos. I'm waiting for a reply soon so I can test it later.
     
  16. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    Ruslank100 and Kurt-Dekker like this.