#How to add a score element to this tutorial's code? (https://www.youtube.com/watch?v=5CW1yGsVg4k)

1 messages · Page 1 of 1 (latest)

viscid geyser
#

I have followed his tutorial and the game works, but I would like to expand it adding a persistent score element.
I have tried adding in a score variable but because of the scripts scene-changing feature to update questions, the value resets.

hushed crown
#

Add this to your GameManager (put it right before the Start function) and the score should be persistent until you destroy the manager or close the game:

void Awake() {
    DontDestroyOnLoad(gameObject);
}```
manic ferry
#

Be careful when using DDOL.
If you reload the scene that the object originally came from, you'll end up with multiple of them.

hushed crown
#

^^^^ forgot to note, ty zen

abstract patrol
#

Op can use playerprefs to save the score

viscid geyser
#

I am using that but because of Brackey's use of reloading scenes to update the questions my text container for the score resets.

abstract patrol
#

Then just need to save the score each time it is increasing or when you want to reload the scene

sullen crest
viscid geyser
#

Thank you for your comment. Does anyone know how to output a score value to a log file which can be referenced in another scene as a text element? I am looking to expand this quiz concept.

abstract patrol
#

Big @sullen crest i know but please lets do with the simplest one

#

When the quiz end store the value using Playerprefs in a function like this

void GameFinish()
{
  PlayerPrefs.SetInt("Score", score);
}

Loading the score in another scene in a start function maybe using this void Start() { score = PlayerPrefs.GetInt("Score"); scoreText.text = score.ToString(); }

manic ferry
abstract patrol
manic ferry
#

Then why'd you bring up PlayerPrefs again, after it was explicitly stated - and acknowledged by you - that it's not a good (and also not the simplest) way? LUL

abstract patrol
manic ferry
#

Perhaps.
But OP mentioned changing scenes in all instances of their question as far as I can see.

abstract patrol
sullen arch
#

Also don't use playerprefs to save score, use json.

#

Player prefs can get reset randomly.

#

Which is why it should only be used for settings(and stuff alike)

manic ferry
manic ferry
sullen arch
manic ferry
# sullen arch They can lol.

Not randomly.
If you reset the registry, or tell Unity to delete all prefs, they'll get lost.
In any other case, it's hard to see how they would reset. After all, PlayerPrefs were designed to persist data.

sullen arch
#

In my experience they can also get restarted after a windows update or simply restarting ur PC.

#

Json is just a much better way for saving data

sullen arch
humble grail
#

Doubt

abstract patrol
viscid geyser
#

My score value updates within the console, but does not appear as a text object, even when I convert it to a string. I am also using PlayerPrefs .

Here is my code for answering a question:

public void UserSelectTrue()
    {
        animator.SetTrigger("True");
        if (currentQuestion.isTrue)
        {
            Debug.Log("Correct");

            score = score + 1; 

            Debug.Log("Score = " + score);
        } else
        {
            Debug.Log("Incorrect");

            score = score - 1;
            scoreText.text = score.ToString();

            Debug.Log("Score = " + score);
        }```
Within my start method:
```csharp
if (PlayerPrefs.HasKey("score")) // Checks if the player has the score key by using PlayerPrefs.
{
    score = PlayerPrefs.GetFloat("score"); // If it exists, retrieve the score value.
}

GameObject scoreUI = GameObject.Find("ScoreUI"); // Find the ScoreUI GameObject in the scene.

Within a function to transition to another question:

PlayerPrefs.SetFloat("score", score);
PlayerPrefs.Save();

As suggested, I am using:

    void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }
abstract patrol
#

Because your function UserSelectTrue() is not call try putting it in the void Update() so that it should be call