Tracking Scores Between Scenes

mattyg55

Member
Joined
Apr 28, 2023
Messages
8
Programming Experience
Beginner
I am trying to take the score from my gameplay scene and display it on my end screen. I am not sure why the"PlayerPrefs" isn't working. I have the code for the score and the code for the score to be displayed at the end screen below. Thanks in advance.
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class score : MonoBehaviour
{

    public TMP_Text output;

    public float count = 0.0f;

    public float speed = 0.001f;
    

    // Start is called before the first frame update
    void Start()
    {
        output.text = "Testing...123";
    }

    // Update is called once per frame
    void Update()
    {
        count = count + 2*speed;

        //cast (int) of a float (1234).0987
        int number = (int)count;

        Debug.Log(count);
        
        //Convert number to a string repreentation of its values
        output.text = "Score: " + number.ToString();

        int score = PlayerPrefs.GetInt("count");
        Debug.Log("Score: " + score);
    }
}
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class finalScore : MonoBehaviour
{
    public TMP_Text finalScoreText;

    void Start()
    {
        // Get the score from the previous scene
        int score = PlayerPrefs.GetInt("count");

        // Update the score text component
        finalScoreText.text = "Final Score: " + score.ToString();


    }
}
 
What does your debug log output from line 38 look like?
 
Screenshot 2023-05-01 at 10.51.48 PM.png
This is what the debug looks like
What does your debug log output from line 38 look like?
 
So if you are calling PlayerPrefs.GetInt("count") on line 38 and getting back 0, why are you surprised that you are also getting 0 when you make the same call elsewhere?
 
Back
Top Bottom