- Home /
How do I call an DontDestroyOnLoad function on UI text
I called an don'tdestroyOnLoad function on two scripts and it doesn't seem to work . I need to carry the ui text over to the next scene. I don't have any errors . I also tried to fix this problem by adding a gameobject tag in the awake function on the test script. I don't know what to do from here. I need help .
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Test : MonoBehaviour {
private MyClockScript myClock;
public GameObject Score;
private ScoreManagerScript scoremanager;
void Awake () {
myClock = GetComponent();
GameObject Score = GameObject.FindWithTag("Tip");
scoremanager = GetComponent();
}
void Update ()
{
if (myClock.m_leftTime < 0 && 0 < ScoreManagerScript.score)
{
SceneManager.LoadScene("j");
}
}
}
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class ScoreManagerScript : MonoBehaviour {
public static int score;
private Text text;
void Awake ()
{
text = GetComponent ();
score = 0;
DontDestroyOnLoad(gameObject);
}
void Update()
{
text.text = "Score: " + score;
Debug.Log("Score: " + score);
}
Select all of your code in the editor then hit the code button that looks like "101010". Correct formatting is an important part to asking questions on here.
If its too much work for people to decipher what your question is asking or what your code does, then most people won't spend the energy to answer the question.
Answer by Sagi02 · Jun 18, 2016 at 07:27 PM
You have to call DontDestroyOnLoad on the root object, here the "canvas" object. What is happening here is that Unity destroys the canvas when loading a scene, which in turn destroys your UI, even though it was marked DontDestroyOnLoad. If you want only some UI elements to get carried over different scenes, you will have to delete the other ones manually.
Answer by importguru88 · Jun 18, 2016 at 11:54 PM
Where at do I call a DontDestroyOnLoad on root canvas ? Delete what I dont understand.
you have to set a reference to the "canvas" object in your script and pass it to DontDestroyOnLoad:
using UnityEngine;
using System.Collections;
public class yourClass : $$anonymous$$onoBehaviour
{
public GameObject rootCanvas;
void Awake()
}
DontDestroyOnLoad(rootCanvas);
}
}
ps: this code isn't proven to be working, it's just a sample, but you get the idea.
Then, in the editor, drag the canvas object into the "rootCanvas" box.
Your answer
Follow this Question
Related Questions
Maintaining Text Size Ratio With Screen 0 Answers
if statement not working for user input 0 Answers
Why will my button not let me run a function? 4 Answers
Quick Question on UI Text Alignment 1 Answer
Positions of UI Text characters. 0 Answers