- Home /
Text UI erases after new scene.
I set up a project where im testing singletons and DontDestroyOnLoad. Im using a public Text type to show my values and Buttons type to change the value between scenes. My problem is when i switch scenes the Text object that i dragged in for the Text type disappears between scenes. I hope this makes scenes to someone. By the way im following a tutorial on Youtube from Unity, but they are using the old GUI system and im trying not to. Heres the link to the video https://www.youtube.com/watch?v=J6FfcJpbPXE
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public static Score points;
public Text myScore;
public int score;
// Use this for initialization
void Awake () {
//DontDestroyOnLoad (gameObject);
myScore.text = ("Score: " + score);
if(points == null){
DontDestroyOnLoad (gameObject);
points = this;
}
else if(points != this)
{
Destroy(gameObject);
}
}
// Update is called once per frame
void Update () {
myScore.text = ("Score: " + score);
}
}
Answer by Chris_Dlala · Jan 23, 2015 at 10:49 PM
Hi, I think the problem is that you have only marked the GameObject with the Score component on it, so when the scene changes Unity will destroy every other object, including the object with the Text component on it. If I remember correctly the child objects underneath the objects marked to not destroy should not be destroyed. You could try moving the Text object to be a child or marking the myScore.gameObject as DontDestroyOnLoad. (Just checked: and DontDestroyOnLoad does keep the whole hierarchy of a gameobject).
Hope that helps =)