#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)
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);
}```
Be careful when using DDOL.
If you reload the scene that the object originally came from, you'll end up with multiple of them.
^^^^ forgot to note, ty zen
Op can use playerprefs to save the score
I am using that but because of Brackey's use of reloading scenes to update the questions my text container for the score resets.
Then just need to save the score each time it is increasing or when you want to reload the scene
PlayerPrefs, as the name implies, is meant for saving preferences
There are other, better, ways to achieve saving...
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.
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(); }
The "simplest one" would be to make the score a static variable. Because those don't get reset until the domain is reloaded - which doesn't happen while the game is running.
Yes this is the most simplest
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? 
Because according to the way i understand i though that he wanted to save the value
Perhaps.
But OP mentioned changing scenes in all instances of their question as far as I can see.
Yeeesss now that I'm reading it again, you can be very correct
Doesn't seem like this is said, but to avoid duplicates, you can check if the object is already in the hierarchy (not active, because incase the object gets disabled it would still spawn another one), this would fix the issue of multiple instances.
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)
How would you check if the object is already in the hierarchy?
And don't say Find now...
...no?
The issue is much more, that they are slow, very limited and don't give control about where things are saved.
On Windows for example, PlayerPrefs will write to the registry.
They can lol.
On the script just check if a instance exists
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.
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
How would this even happen
Idk tbh, I just know it has occured.
Doubt
In how many years of expérience will i see that miraculous event ?
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);
}
Because your function UserSelectTrue() is not call try putting it in the void Update() so that it should be call