The question is answered, right answer was accepted
In the next scene tell player there score out of total using player prefab before next level
In my game i have it so when the player picks up an item the score increases ,there is a max number of items the level lets say ten , at the end of the level a new scene is loaded with some text and a button to load the next level what i'm looking to do is have the text say
" You scored (what they scored )out of 10 "
Is there a way to go about doing that.
here is the code im using to mange the score
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
static public int score;
public Text scoreText;
void Start ()
{
PlayerPrefs.GetInt ("scorePrefabs");
score=PlayerPrefs.GetInt ("scorePrefabs");
}
void Update ()
{
if (scoreText.name == "scoreText")
{
scoreText.text = "score: " + score;
}
PlayerPrefs.SetInt ("scorePrefabs", score);
}
}
(I am new to coding, Unity and game deign if i'm asking this poorly or in the wrong place i apologize and would be thankful to be pointer in the right direction )
Answer by Intare · Jan 12, 2017 at 10:11 AM
In the scene of the game, you can do something like:
private int score;
void OnDestroy () {
//Function called when changing scenes Or destroying gameobject PlayerPrefs.SetInt("Score", score);
}
And, in the next scene:
public Text text;
void Start () {
int score = PlayerPrefs.GetInt("Score", 0);
text.text = "You scored " + score.ToString() + " out of 10";
}
This isn't quite working the message is now co$$anonymous$$g up but its always says 0 is the code you shown me meant to work with my code or on it own? I'm going to keep working on it but if you have any idea what may need changing please let me know thank for replying to my question
Put a print message on the OnDestroy method to see if it's called. If not, there are the problem. It's mean to work alone, no with your code.
thanks a million been trying to get it work 4 days straight
Follow this Question
Related Questions
How to get the active/loaded Scene then turn the name of it into a string? [C#] 1 Answer
A way to tell the player what they scored in the next scene 1 Answer
SetActive dosen't work 0 Answers
Saving an image's state 2 Answers
In the next scene show the score using player prefab before next level 1 Answer