- Home /
Carry over texts from one scene to another?
Hello. I am making a game that is 2D and for Android. I recently made a post asking how to set a score to zero using a button. Before the post, I made the score update every second and the text would carry over. Now the score is only updated every time you score a point, which is good, and pressing the button does make it to zero. The score does transfer over, but the text doesn't actually show it. What I mean is that it knows how many points are scored but the text just says New Text instead of saying Score: 24 ( For example ) What needs to be done for it to show the score instead of saying New Text?
The scoremanager script is
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static int score; // The player's score.
Text text; // Reference to the Text component.
void Awake ()
{
text = GetComponent <Text> ();
}
public void SetScore (int newScore)
{
ScoreManager.score = newScore; //update the static score variable
text.text = "Score: " + score;
}
}
and the script that actually detects for a point to be added is
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public int scoreValue = 1;
public ScoreManager scoreManager;
void OnMouseDown () {
scoreManager.SetScore(ScoreManager.score + scoreValue);
}
}
so when you debug newScore after line 17 , do you get the updated "new score" ?
Yes I do. It's just that the script I am using only updates when a point is scored. Which means the text won't update when the scene changes. Unless somehow a point was added then it would show the score.
Answer by Digital-Phantom · Apr 06, 2015 at 07:16 PM
Just use DoNotDestroyOnLoad to carry the scoremanager object over from scene to scene.
Okay... I've done that. That doesn't change anything. I need to figure out a way for it to update the text to display the score. Since the text will only update if the score changes.
Your answer
![](https://koobas.hobune.stream/wayback/20220613192345im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnLevelWasLoaded script problem. 1 Answer
Score system 1 Answer
Why won't the Score reset? 2 Answers
Collision On Object Error. Please help. 1 Answer
Scene keeps reloading, can't keep score 3 Answers