So im trying to make a score value that i can add to and it has to be saved in playerprefs. I want to load it in a different scene and display it there with a 2D text. How would i go about doing that. I would love an detailed tutorial for that. Example:
What objects do i put the script in?
What scripts to liad it what to save it?
Etc.
Here is an official tutorial for using PlayerPrefs for a high score-- not sure if that’s exactly what you’re going for, but it should be close. Please give it a look and feel free to ask questions about it.
I am not going for high score
But i think i can use it.
You should really consider doing some tutorials… I realized you were asking for a specific one, but just go under the Learn section in the Unity site. This is kind of a basic question. There’s nothing at all wrong with asking basic questions though, but that tells me you can probably benefit from doing a few end-to-end tutorials. Not just specific topics, but create a few basic games, like they walk you through on the Unity tutorial site: Unity Learn
Anyways, this one here from there is probably right up your alley:
Specifically, here’s the part about having a score that updates on screen:
It’s been quite a while since I’ve done this tutorial, so I’m not sure if they mention this, but in order for your score to remain set across various levels, you’ll need to ensure that the object that manages the score persists (isn’t destroyed) as a new scene is loaded… Unity - Scripting API: Object.DontDestroyOnLoad
As others stated, a few tutorials are probably in order, but here’s a detailed example of how to achieve what you desire.
Put this on a gameobject that will be in the first scene your game loads.
using UnityEngine;
public class ScoreManager : MonoBehaviour
{
//static variables persist through scene changes.
public static ScoreManager Instance;
int score;
void Awake ()
{
//Does an instance of "ScoreManager" currently exists?
if (Instance != null)
{
//It does, let's destroy this instance since it's not needed
Destroy (gameObject);
}
else
{
//It doesn't. Let's define this instance as the static instance
Instance = this;
//Let's make it so this object won't be destroyed during scene changes
DontDestroyOnLoad (gameObject);
//We set the initial value of score as the value we had stored in the player prefs
score = PlayerPrefs.GetInt ("score");
}
}
//Call this method to add to the current score.
public void AddToScore (int scoreToAdd)
{
//We add to the local variable of score
score += scoreToAdd;
//We update the player prefs score with the new value
PlayerPrefs.SetInt ("score", score);
//We save the player prefs
PlayerPrefs.Save ();
}
//Call this method to get the current score
public int GetScore ()
{
//This returns the local score variable. Use this to fetch and display the current score.
return score;
}
}
Did you try using : public static int myInt;
Antoine, im using your code. Thanks. The only problem is. I have no idea how i can add a specific amount to the score… does it not work in editors play mode or am i missing something? If anyone can help me id really appreciate it:)
From another script you would call:
// 5 is just an example, you decide how much score to add
ScoreManager.Instance.AddToScore(5);
ill try that! Thanks!